audit.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /* audit.c -- Auditing support
  2. * Gateway between the kernel (e.g., selinux) and the user-space audit daemon.
  3. * System-call specific features have moved to auditsc.c
  4. *
  5. * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina.
  6. * All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  23. *
  24. * Goals: 1) Integrate fully with Security Modules.
  25. * 2) Minimal run-time overhead:
  26. * a) Minimal when syscall auditing is disabled (audit_enable=0).
  27. * b) Small when syscall auditing is enabled and no audit record
  28. * is generated (defer as much work as possible to record
  29. * generation time):
  30. * i) context is allocated,
  31. * ii) names from getname are stored without a copy, and
  32. * iii) inode information stored from path_lookup.
  33. * 3) Ability to disable syscall auditing at boot time (audit=0).
  34. * 4) Usable by other parts of the kernel (if audit_log* is called,
  35. * then a syscall record will be generated automatically for the
  36. * current syscall).
  37. * 5) Netlink interface to user-space.
  38. * 6) Support low-overhead kernel-based filtering to minimize the
  39. * information that must be passed to user-space.
  40. *
  41. * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
  42. */
  43. #include <linux/init.h>
  44. #include <asm/types.h>
  45. #include <asm/atomic.h>
  46. #include <linux/mm.h>
  47. #include <linux/module.h>
  48. #include <linux/slab.h>
  49. #include <linux/err.h>
  50. #include <linux/kthread.h>
  51. #include <linux/audit.h>
  52. #include <net/sock.h>
  53. #include <net/netlink.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/netlink.h>
  56. #include <linux/freezer.h>
  57. #include <linux/tty.h>
  58. #include "audit.h"
  59. /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED.
  60. * (Initialization happens after skb_init is called.) */
  61. #define AUDIT_DISABLED -1
  62. #define AUDIT_UNINITIALIZED 0
  63. #define AUDIT_INITIALIZED 1
  64. static int audit_initialized;
  65. #define AUDIT_OFF 0
  66. #define AUDIT_ON 1
  67. #define AUDIT_LOCKED 2
  68. int audit_enabled;
  69. int audit_ever_enabled;
  70. EXPORT_SYMBOL_GPL(audit_enabled);
  71. /* Default state when kernel boots without any parameters. */
  72. static int audit_default;
  73. /* If auditing cannot proceed, audit_failure selects what happens. */
  74. static int audit_failure = AUDIT_FAIL_PRINTK;
  75. /*
  76. * If audit records are to be written to the netlink socket, audit_pid
  77. * contains the pid of the auditd process and audit_nlk_pid contains
  78. * the pid to use to send netlink messages to that process.
  79. */
  80. int audit_pid;
  81. static int audit_nlk_pid;
  82. /* If audit_rate_limit is non-zero, limit the rate of sending audit records
  83. * to that number per second. This prevents DoS attacks, but results in
  84. * audit records being dropped. */
  85. static int audit_rate_limit;
  86. /* Number of outstanding audit_buffers allowed. */
  87. static int audit_backlog_limit = 64;
  88. static int audit_backlog_wait_time = 60 * HZ;
  89. static int audit_backlog_wait_overflow = 0;
  90. /* The identity of the user shutting down the audit system. */
  91. uid_t audit_sig_uid = -1;
  92. pid_t audit_sig_pid = -1;
  93. u32 audit_sig_sid = 0;
  94. /* Records can be lost in several ways:
  95. 0) [suppressed in audit_alloc]
  96. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  97. 2) out of memory in audit_log_move [alloc_skb]
  98. 3) suppressed due to audit_rate_limit
  99. 4) suppressed due to audit_backlog_limit
  100. */
  101. static atomic_t audit_lost = ATOMIC_INIT(0);
  102. /* The netlink socket. */
  103. static struct sock *audit_sock;
  104. /* Hash for inode-based rules */
  105. struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
  106. /* The audit_freelist is a list of pre-allocated audit buffers (if more
  107. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  108. * being placed on the freelist). */
  109. static DEFINE_SPINLOCK(audit_freelist_lock);
  110. static int audit_freelist_count;
  111. static LIST_HEAD(audit_freelist);
  112. static struct sk_buff_head audit_skb_queue;
  113. /* queue of skbs to send to auditd when/if it comes back */
  114. static struct sk_buff_head audit_skb_hold_queue;
  115. static struct task_struct *kauditd_task;
  116. static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
  117. static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
  118. /* Serialize requests from userspace. */
  119. DEFINE_MUTEX(audit_cmd_mutex);
  120. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  121. * audit records. Since printk uses a 1024 byte buffer, this buffer
  122. * should be at least that large. */
  123. #define AUDIT_BUFSIZ 1024
  124. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  125. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  126. #define AUDIT_MAXFREE (2*NR_CPUS)
  127. /* The audit_buffer is used when formatting an audit record. The caller
  128. * locks briefly to get the record off the freelist or to allocate the
  129. * buffer, and locks briefly to send the buffer to the netlink layer or
  130. * to place it on a transmit queue. Multiple audit_buffers can be in
  131. * use simultaneously. */
  132. struct audit_buffer {
  133. struct list_head list;
  134. struct sk_buff *skb; /* formatted skb ready to send */
  135. struct audit_context *ctx; /* NULL or associated context */
  136. gfp_t gfp_mask;
  137. };
  138. struct audit_reply {
  139. int pid;
  140. struct sk_buff *skb;
  141. };
  142. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  143. {
  144. if (ab) {
  145. struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
  146. nlh->nlmsg_pid = pid;
  147. }
  148. }
  149. void audit_panic(const char *message)
  150. {
  151. switch (audit_failure)
  152. {
  153. case AUDIT_FAIL_SILENT:
  154. break;
  155. case AUDIT_FAIL_PRINTK:
  156. if (printk_ratelimit())
  157. printk(KERN_ERR "audit: %s\n", message);
  158. break;
  159. case AUDIT_FAIL_PANIC:
  160. /* test audit_pid since printk is always losey, why bother? */
  161. if (audit_pid)
  162. panic("audit: %s\n", message);
  163. break;
  164. }
  165. }
  166. static inline int audit_rate_check(void)
  167. {
  168. static unsigned long last_check = 0;
  169. static int messages = 0;
  170. static DEFINE_SPINLOCK(lock);
  171. unsigned long flags;
  172. unsigned long now;
  173. unsigned long elapsed;
  174. int retval = 0;
  175. if (!audit_rate_limit) return 1;
  176. spin_lock_irqsave(&lock, flags);
  177. if (++messages < audit_rate_limit) {
  178. retval = 1;
  179. } else {
  180. now = jiffies;
  181. elapsed = now - last_check;
  182. if (elapsed > HZ) {
  183. last_check = now;
  184. messages = 0;
  185. retval = 1;
  186. }
  187. }
  188. spin_unlock_irqrestore(&lock, flags);
  189. return retval;
  190. }
  191. /**
  192. * audit_log_lost - conditionally log lost audit message event
  193. * @message: the message stating reason for lost audit message
  194. *
  195. * Emit at least 1 message per second, even if audit_rate_check is
  196. * throttling.
  197. * Always increment the lost messages counter.
  198. */
  199. void audit_log_lost(const char *message)
  200. {
  201. static unsigned long last_msg = 0;
  202. static DEFINE_SPINLOCK(lock);
  203. unsigned long flags;
  204. unsigned long now;
  205. int print;
  206. atomic_inc(&audit_lost);
  207. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  208. if (!print) {
  209. spin_lock_irqsave(&lock, flags);
  210. now = jiffies;
  211. if (now - last_msg > HZ) {
  212. print = 1;
  213. last_msg = now;
  214. }
  215. spin_unlock_irqrestore(&lock, flags);
  216. }
  217. if (print) {
  218. if (printk_ratelimit())
  219. printk(KERN_WARNING
  220. "audit: audit_lost=%d audit_rate_limit=%d "
  221. "audit_backlog_limit=%d\n",
  222. atomic_read(&audit_lost),
  223. audit_rate_limit,
  224. audit_backlog_limit);
  225. audit_panic(message);
  226. }
  227. }
  228. static int audit_log_config_change(char *function_name, int new, int old,
  229. uid_t loginuid, u32 sessionid, u32 sid,
  230. int allow_changes)
  231. {
  232. struct audit_buffer *ab;
  233. int rc = 0;
  234. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  235. audit_log_format(ab, "%s=%d old=%d auid=%u ses=%u", function_name, new,
  236. old, loginuid, sessionid);
  237. if (sid) {
  238. char *ctx = NULL;
  239. u32 len;
  240. rc = security_secid_to_secctx(sid, &ctx, &len);
  241. if (rc) {
  242. audit_log_format(ab, " sid=%u", sid);
  243. allow_changes = 0; /* Something weird, deny request */
  244. } else {
  245. audit_log_format(ab, " subj=%s", ctx);
  246. security_release_secctx(ctx, len);
  247. }
  248. }
  249. audit_log_format(ab, " res=%d", allow_changes);
  250. audit_log_end(ab);
  251. return rc;
  252. }
  253. static int audit_do_config_change(char *function_name, int *to_change,
  254. int new, uid_t loginuid, u32 sessionid,
  255. u32 sid)
  256. {
  257. int allow_changes, rc = 0, old = *to_change;
  258. /* check if we are locked */
  259. if (audit_enabled == AUDIT_LOCKED)
  260. allow_changes = 0;
  261. else
  262. allow_changes = 1;
  263. if (audit_enabled != AUDIT_OFF) {
  264. rc = audit_log_config_change(function_name, new, old, loginuid,
  265. sessionid, sid, allow_changes);
  266. if (rc)
  267. allow_changes = 0;
  268. }
  269. /* If we are allowed, make the change */
  270. if (allow_changes == 1)
  271. *to_change = new;
  272. /* Not allowed, update reason */
  273. else if (rc == 0)
  274. rc = -EPERM;
  275. return rc;
  276. }
  277. static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sessionid,
  278. u32 sid)
  279. {
  280. return audit_do_config_change("audit_rate_limit", &audit_rate_limit,
  281. limit, loginuid, sessionid, sid);
  282. }
  283. static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sessionid,
  284. u32 sid)
  285. {
  286. return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit,
  287. limit, loginuid, sessionid, sid);
  288. }
  289. static int audit_set_enabled(int state, uid_t loginuid, u32 sessionid, u32 sid)
  290. {
  291. int rc;
  292. if (state < AUDIT_OFF || state > AUDIT_LOCKED)
  293. return -EINVAL;
  294. rc = audit_do_config_change("audit_enabled", &audit_enabled, state,
  295. loginuid, sessionid, sid);
  296. if (!rc)
  297. audit_ever_enabled |= !!state;
  298. return rc;
  299. }
  300. static int audit_set_failure(int state, uid_t loginuid, u32 sessionid, u32 sid)
  301. {
  302. if (state != AUDIT_FAIL_SILENT
  303. && state != AUDIT_FAIL_PRINTK
  304. && state != AUDIT_FAIL_PANIC)
  305. return -EINVAL;
  306. return audit_do_config_change("audit_failure", &audit_failure, state,
  307. loginuid, sessionid, sid);
  308. }
  309. /*
  310. * Queue skbs to be sent to auditd when/if it comes back. These skbs should
  311. * already have been sent via prink/syslog and so if these messages are dropped
  312. * it is not a huge concern since we already passed the audit_log_lost()
  313. * notification and stuff. This is just nice to get audit messages during
  314. * boot before auditd is running or messages generated while auditd is stopped.
  315. * This only holds messages is audit_default is set, aka booting with audit=1
  316. * or building your kernel that way.
  317. */
  318. static void audit_hold_skb(struct sk_buff *skb)
  319. {
  320. if (audit_default &&
  321. skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit)
  322. skb_queue_tail(&audit_skb_hold_queue, skb);
  323. else
  324. kfree_skb(skb);
  325. }
  326. /*
  327. * For one reason or another this nlh isn't getting delivered to the userspace
  328. * audit daemon, just send it to printk.
  329. */
  330. static void audit_printk_skb(struct sk_buff *skb)
  331. {
  332. struct nlmsghdr *nlh = nlmsg_hdr(skb);
  333. char *data = NLMSG_DATA(nlh);
  334. if (nlh->nlmsg_type != AUDIT_EOE) {
  335. if (printk_ratelimit())
  336. printk(KERN_NOTICE "type=%d %s\n", nlh->nlmsg_type, data);
  337. else
  338. audit_log_lost("printk limit exceeded\n");
  339. }
  340. audit_hold_skb(skb);
  341. }
  342. static void kauditd_send_skb(struct sk_buff *skb)
  343. {
  344. int err;
  345. /* take a reference in case we can't send it and we want to hold it */
  346. skb_get(skb);
  347. err = netlink_unicast(audit_sock, skb, audit_nlk_pid, 0);
  348. if (err < 0) {
  349. BUG_ON(err != -ECONNREFUSED); /* Shouldn't happen */
  350. printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
  351. audit_log_lost("auditd disappeared\n");
  352. audit_pid = 0;
  353. /* we might get lucky and get this in the next auditd */
  354. audit_hold_skb(skb);
  355. } else
  356. /* drop the extra reference if sent ok */
  357. consume_skb(skb);
  358. }
  359. static int kauditd_thread(void *dummy)
  360. {
  361. struct sk_buff *skb;
  362. set_freezable();
  363. while (!kthread_should_stop()) {
  364. /*
  365. * if auditd just started drain the queue of messages already
  366. * sent to syslog/printk. remember loss here is ok. we already
  367. * called audit_log_lost() if it didn't go out normally. so the
  368. * race between the skb_dequeue and the next check for audit_pid
  369. * doesn't matter.
  370. *
  371. * if you ever find kauditd to be too slow we can get a perf win
  372. * by doing our own locking and keeping better track if there
  373. * are messages in this queue. I don't see the need now, but
  374. * in 5 years when I want to play with this again I'll see this
  375. * note and still have no friggin idea what i'm thinking today.
  376. */
  377. if (audit_default && audit_pid) {
  378. skb = skb_dequeue(&audit_skb_hold_queue);
  379. if (unlikely(skb)) {
  380. while (skb && audit_pid) {
  381. kauditd_send_skb(skb);
  382. skb = skb_dequeue(&audit_skb_hold_queue);
  383. }
  384. }
  385. }
  386. skb = skb_dequeue(&audit_skb_queue);
  387. wake_up(&audit_backlog_wait);
  388. if (skb) {
  389. if (audit_pid)
  390. kauditd_send_skb(skb);
  391. else
  392. audit_printk_skb(skb);
  393. } else {
  394. DECLARE_WAITQUEUE(wait, current);
  395. set_current_state(TASK_INTERRUPTIBLE);
  396. add_wait_queue(&kauditd_wait, &wait);
  397. if (!skb_queue_len(&audit_skb_queue)) {
  398. try_to_freeze();
  399. schedule();
  400. }
  401. __set_current_state(TASK_RUNNING);
  402. remove_wait_queue(&kauditd_wait, &wait);
  403. }
  404. }
  405. return 0;
  406. }
  407. static int audit_prepare_user_tty(pid_t pid, uid_t loginuid, u32 sessionid)
  408. {
  409. struct task_struct *tsk;
  410. int err;
  411. rcu_read_lock();
  412. tsk = find_task_by_vpid(pid);
  413. if (!tsk) {
  414. rcu_read_unlock();
  415. return -ESRCH;
  416. }
  417. get_task_struct(tsk);
  418. rcu_read_unlock();
  419. err = tty_audit_push_task(tsk, loginuid, sessionid);
  420. put_task_struct(tsk);
  421. return err;
  422. }
  423. int audit_send_list(void *_dest)
  424. {
  425. struct audit_netlink_list *dest = _dest;
  426. int pid = dest->pid;
  427. struct sk_buff *skb;
  428. /* wait for parent to finish and send an ACK */
  429. mutex_lock(&audit_cmd_mutex);
  430. mutex_unlock(&audit_cmd_mutex);
  431. while ((skb = __skb_dequeue(&dest->q)) != NULL)
  432. netlink_unicast(audit_sock, skb, pid, 0);
  433. kfree(dest);
  434. return 0;
  435. }
  436. struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
  437. int multi, const void *payload, int size)
  438. {
  439. struct sk_buff *skb;
  440. struct nlmsghdr *nlh;
  441. void *data;
  442. int flags = multi ? NLM_F_MULTI : 0;
  443. int t = done ? NLMSG_DONE : type;
  444. skb = nlmsg_new(size, GFP_KERNEL);
  445. if (!skb)
  446. return NULL;
  447. nlh = NLMSG_NEW(skb, pid, seq, t, size, flags);
  448. data = NLMSG_DATA(nlh);
  449. memcpy(data, payload, size);
  450. return skb;
  451. nlmsg_failure: /* Used by NLMSG_NEW */
  452. if (skb)
  453. kfree_skb(skb);
  454. return NULL;
  455. }
  456. static int audit_send_reply_thread(void *arg)
  457. {
  458. struct audit_reply *reply = (struct audit_reply *)arg;
  459. mutex_lock(&audit_cmd_mutex);
  460. mutex_unlock(&audit_cmd_mutex);
  461. /* Ignore failure. It'll only happen if the sender goes away,
  462. because our timeout is set to infinite. */
  463. netlink_unicast(audit_sock, reply->skb, reply->pid, 0);
  464. kfree(reply);
  465. return 0;
  466. }
  467. /**
  468. * audit_send_reply - send an audit reply message via netlink
  469. * @pid: process id to send reply to
  470. * @seq: sequence number
  471. * @type: audit message type
  472. * @done: done (last) flag
  473. * @multi: multi-part message flag
  474. * @payload: payload data
  475. * @size: payload size
  476. *
  477. * Allocates an skb, builds the netlink message, and sends it to the pid.
  478. * No failure notifications.
  479. */
  480. static void audit_send_reply(int pid, int seq, int type, int done, int multi,
  481. const void *payload, int size)
  482. {
  483. struct sk_buff *skb;
  484. struct task_struct *tsk;
  485. struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
  486. GFP_KERNEL);
  487. if (!reply)
  488. return;
  489. skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
  490. if (!skb)
  491. goto out;
  492. reply->pid = pid;
  493. reply->skb = skb;
  494. tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
  495. if (!IS_ERR(tsk))
  496. return;
  497. kfree_skb(skb);
  498. out:
  499. kfree(reply);
  500. }
  501. /*
  502. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  503. * control messages.
  504. */
  505. static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
  506. {
  507. int err = 0;
  508. switch (msg_type) {
  509. case AUDIT_GET:
  510. case AUDIT_LIST:
  511. case AUDIT_LIST_RULES:
  512. case AUDIT_SET:
  513. case AUDIT_ADD:
  514. case AUDIT_ADD_RULE:
  515. case AUDIT_DEL:
  516. case AUDIT_DEL_RULE:
  517. case AUDIT_SIGNAL_INFO:
  518. case AUDIT_TTY_GET:
  519. case AUDIT_TTY_SET:
  520. case AUDIT_TRIM:
  521. case AUDIT_MAKE_EQUIV:
  522. if (security_netlink_recv(skb, CAP_AUDIT_CONTROL))
  523. err = -EPERM;
  524. break;
  525. case AUDIT_USER:
  526. case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
  527. case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
  528. if (security_netlink_recv(skb, CAP_AUDIT_WRITE))
  529. err = -EPERM;
  530. break;
  531. default: /* bad msg */
  532. err = -EINVAL;
  533. }
  534. return err;
  535. }
  536. static int audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type,
  537. u32 pid, u32 uid, uid_t auid, u32 ses,
  538. u32 sid)
  539. {
  540. int rc = 0;
  541. char *ctx = NULL;
  542. u32 len;
  543. if (!audit_enabled) {
  544. *ab = NULL;
  545. return rc;
  546. }
  547. *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  548. audit_log_format(*ab, "user pid=%d uid=%u auid=%u ses=%u",
  549. pid, uid, auid, ses);
  550. if (sid) {
  551. rc = security_secid_to_secctx(sid, &ctx, &len);
  552. if (rc)
  553. audit_log_format(*ab, " ssid=%u", sid);
  554. else {
  555. audit_log_format(*ab, " subj=%s", ctx);
  556. security_release_secctx(ctx, len);
  557. }
  558. }
  559. return rc;
  560. }
  561. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  562. {
  563. u32 uid, pid, seq, sid;
  564. void *data;
  565. struct audit_status *status_get, status_set;
  566. int err;
  567. struct audit_buffer *ab;
  568. u16 msg_type = nlh->nlmsg_type;
  569. uid_t loginuid; /* loginuid of sender */
  570. u32 sessionid;
  571. struct audit_sig_info *sig_data;
  572. char *ctx = NULL;
  573. u32 len;
  574. err = audit_netlink_ok(skb, msg_type);
  575. if (err)
  576. return err;
  577. /* As soon as there's any sign of userspace auditd,
  578. * start kauditd to talk to it */
  579. if (!kauditd_task)
  580. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  581. if (IS_ERR(kauditd_task)) {
  582. err = PTR_ERR(kauditd_task);
  583. kauditd_task = NULL;
  584. return err;
  585. }
  586. pid = NETLINK_CREDS(skb)->pid;
  587. uid = NETLINK_CREDS(skb)->uid;
  588. loginuid = audit_get_loginuid(current);
  589. sessionid = audit_get_sessionid(current);
  590. security_task_getsecid(current, &sid);
  591. seq = nlh->nlmsg_seq;
  592. data = NLMSG_DATA(nlh);
  593. switch (msg_type) {
  594. case AUDIT_GET:
  595. status_set.enabled = audit_enabled;
  596. status_set.failure = audit_failure;
  597. status_set.pid = audit_pid;
  598. status_set.rate_limit = audit_rate_limit;
  599. status_set.backlog_limit = audit_backlog_limit;
  600. status_set.lost = atomic_read(&audit_lost);
  601. status_set.backlog = skb_queue_len(&audit_skb_queue);
  602. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  603. &status_set, sizeof(status_set));
  604. break;
  605. case AUDIT_SET:
  606. if (nlh->nlmsg_len < sizeof(struct audit_status))
  607. return -EINVAL;
  608. status_get = (struct audit_status *)data;
  609. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  610. err = audit_set_enabled(status_get->enabled,
  611. loginuid, sessionid, sid);
  612. if (err < 0)
  613. return err;
  614. }
  615. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  616. err = audit_set_failure(status_get->failure,
  617. loginuid, sessionid, sid);
  618. if (err < 0)
  619. return err;
  620. }
  621. if (status_get->mask & AUDIT_STATUS_PID) {
  622. int new_pid = status_get->pid;
  623. if (audit_enabled != AUDIT_OFF)
  624. audit_log_config_change("audit_pid", new_pid,
  625. audit_pid, loginuid,
  626. sessionid, sid, 1);
  627. audit_pid = new_pid;
  628. audit_nlk_pid = NETLINK_CB(skb).pid;
  629. }
  630. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) {
  631. err = audit_set_rate_limit(status_get->rate_limit,
  632. loginuid, sessionid, sid);
  633. if (err < 0)
  634. return err;
  635. }
  636. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  637. err = audit_set_backlog_limit(status_get->backlog_limit,
  638. loginuid, sessionid, sid);
  639. break;
  640. case AUDIT_USER:
  641. case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
  642. case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
  643. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  644. return 0;
  645. err = audit_filter_user(&NETLINK_CB(skb));
  646. if (err == 1) {
  647. err = 0;
  648. if (msg_type == AUDIT_USER_TTY) {
  649. err = audit_prepare_user_tty(pid, loginuid,
  650. sessionid);
  651. if (err)
  652. break;
  653. }
  654. audit_log_common_recv_msg(&ab, msg_type, pid, uid,
  655. loginuid, sessionid, sid);
  656. if (msg_type != AUDIT_USER_TTY)
  657. audit_log_format(ab, " msg='%.1024s'",
  658. (char *)data);
  659. else {
  660. int size;
  661. audit_log_format(ab, " msg=");
  662. size = nlmsg_len(nlh);
  663. if (size > 0 &&
  664. ((unsigned char *)data)[size - 1] == '\0')
  665. size--;
  666. audit_log_n_untrustedstring(ab, data, size);
  667. }
  668. audit_set_pid(ab, pid);
  669. audit_log_end(ab);
  670. }
  671. break;
  672. case AUDIT_ADD:
  673. case AUDIT_DEL:
  674. if (nlmsg_len(nlh) < sizeof(struct audit_rule))
  675. return -EINVAL;
  676. if (audit_enabled == AUDIT_LOCKED) {
  677. audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
  678. uid, loginuid, sessionid, sid);
  679. audit_log_format(ab, " audit_enabled=%d res=0",
  680. audit_enabled);
  681. audit_log_end(ab);
  682. return -EPERM;
  683. }
  684. /* fallthrough */
  685. case AUDIT_LIST:
  686. err = audit_receive_filter(msg_type, NETLINK_CB(skb).pid,
  687. uid, seq, data, nlmsg_len(nlh),
  688. loginuid, sessionid, sid);
  689. break;
  690. case AUDIT_ADD_RULE:
  691. case AUDIT_DEL_RULE:
  692. if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
  693. return -EINVAL;
  694. if (audit_enabled == AUDIT_LOCKED) {
  695. audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
  696. uid, loginuid, sessionid, sid);
  697. audit_log_format(ab, " audit_enabled=%d res=0",
  698. audit_enabled);
  699. audit_log_end(ab);
  700. return -EPERM;
  701. }
  702. /* fallthrough */
  703. case AUDIT_LIST_RULES:
  704. err = audit_receive_filter(msg_type, NETLINK_CB(skb).pid,
  705. uid, seq, data, nlmsg_len(nlh),
  706. loginuid, sessionid, sid);
  707. break;
  708. case AUDIT_TRIM:
  709. audit_trim_trees();
  710. audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
  711. uid, loginuid, sessionid, sid);
  712. audit_log_format(ab, " op=trim res=1");
  713. audit_log_end(ab);
  714. break;
  715. case AUDIT_MAKE_EQUIV: {
  716. void *bufp = data;
  717. u32 sizes[2];
  718. size_t msglen = nlmsg_len(nlh);
  719. char *old, *new;
  720. err = -EINVAL;
  721. if (msglen < 2 * sizeof(u32))
  722. break;
  723. memcpy(sizes, bufp, 2 * sizeof(u32));
  724. bufp += 2 * sizeof(u32);
  725. msglen -= 2 * sizeof(u32);
  726. old = audit_unpack_string(&bufp, &msglen, sizes[0]);
  727. if (IS_ERR(old)) {
  728. err = PTR_ERR(old);
  729. break;
  730. }
  731. new = audit_unpack_string(&bufp, &msglen, sizes[1]);
  732. if (IS_ERR(new)) {
  733. err = PTR_ERR(new);
  734. kfree(old);
  735. break;
  736. }
  737. /* OK, here comes... */
  738. err = audit_tag_tree(old, new);
  739. audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
  740. uid, loginuid, sessionid, sid);
  741. audit_log_format(ab, " op=make_equiv old=");
  742. audit_log_untrustedstring(ab, old);
  743. audit_log_format(ab, " new=");
  744. audit_log_untrustedstring(ab, new);
  745. audit_log_format(ab, " res=%d", !err);
  746. audit_log_end(ab);
  747. kfree(old);
  748. kfree(new);
  749. break;
  750. }
  751. case AUDIT_SIGNAL_INFO:
  752. len = 0;
  753. if (audit_sig_sid) {
  754. err = security_secid_to_secctx(audit_sig_sid, &ctx, &len);
  755. if (err)
  756. return err;
  757. }
  758. sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
  759. if (!sig_data) {
  760. if (audit_sig_sid)
  761. security_release_secctx(ctx, len);
  762. return -ENOMEM;
  763. }
  764. sig_data->uid = audit_sig_uid;
  765. sig_data->pid = audit_sig_pid;
  766. if (audit_sig_sid) {
  767. memcpy(sig_data->ctx, ctx, len);
  768. security_release_secctx(ctx, len);
  769. }
  770. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  771. 0, 0, sig_data, sizeof(*sig_data) + len);
  772. kfree(sig_data);
  773. break;
  774. case AUDIT_TTY_GET: {
  775. struct audit_tty_status s;
  776. struct task_struct *tsk;
  777. unsigned long flags;
  778. rcu_read_lock();
  779. tsk = find_task_by_vpid(pid);
  780. if (tsk && lock_task_sighand(tsk, &flags)) {
  781. s.enabled = tsk->signal->audit_tty != 0;
  782. unlock_task_sighand(tsk, &flags);
  783. } else
  784. err = -ESRCH;
  785. rcu_read_unlock();
  786. if (!err)
  787. audit_send_reply(NETLINK_CB(skb).pid, seq,
  788. AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
  789. break;
  790. }
  791. case AUDIT_TTY_SET: {
  792. struct audit_tty_status *s;
  793. struct task_struct *tsk;
  794. unsigned long flags;
  795. if (nlh->nlmsg_len < sizeof(struct audit_tty_status))
  796. return -EINVAL;
  797. s = data;
  798. if (s->enabled != 0 && s->enabled != 1)
  799. return -EINVAL;
  800. rcu_read_lock();
  801. tsk = find_task_by_vpid(pid);
  802. if (tsk && lock_task_sighand(tsk, &flags)) {
  803. tsk->signal->audit_tty = s->enabled != 0;
  804. unlock_task_sighand(tsk, &flags);
  805. } else
  806. err = -ESRCH;
  807. rcu_read_unlock();
  808. break;
  809. }
  810. default:
  811. err = -EINVAL;
  812. break;
  813. }
  814. return err < 0 ? err : 0;
  815. }
  816. /*
  817. * Get message from skb. Each message is processed by audit_receive_msg.
  818. * Malformed skbs with wrong length are discarded silently.
  819. */
  820. static void audit_receive_skb(struct sk_buff *skb)
  821. {
  822. struct nlmsghdr *nlh;
  823. /*
  824. * len MUST be signed for NLMSG_NEXT to be able to dec it below 0
  825. * if the nlmsg_len was not aligned
  826. */
  827. int len;
  828. int err;
  829. nlh = nlmsg_hdr(skb);
  830. len = skb->len;
  831. while (NLMSG_OK(nlh, len)) {
  832. err = audit_receive_msg(skb, nlh);
  833. /* if err or if this message says it wants a response */
  834. if (err || (nlh->nlmsg_flags & NLM_F_ACK))
  835. netlink_ack(skb, nlh, err);
  836. nlh = NLMSG_NEXT(nlh, len);
  837. }
  838. }
  839. /* Receive messages from netlink socket. */
  840. static void audit_receive(struct sk_buff *skb)
  841. {
  842. mutex_lock(&audit_cmd_mutex);
  843. audit_receive_skb(skb);
  844. mutex_unlock(&audit_cmd_mutex);
  845. }
  846. /* Initialize audit support at boot time. */
  847. static int __init audit_init(void)
  848. {
  849. int i;
  850. if (audit_initialized == AUDIT_DISABLED)
  851. return 0;
  852. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  853. audit_default ? "enabled" : "disabled");
  854. audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, 0,
  855. audit_receive, NULL, THIS_MODULE);
  856. if (!audit_sock)
  857. audit_panic("cannot initialize netlink socket");
  858. else
  859. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  860. skb_queue_head_init(&audit_skb_queue);
  861. skb_queue_head_init(&audit_skb_hold_queue);
  862. audit_initialized = AUDIT_INITIALIZED;
  863. audit_enabled = audit_default;
  864. audit_ever_enabled |= !!audit_default;
  865. audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
  866. for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
  867. INIT_LIST_HEAD(&audit_inode_hash[i]);
  868. return 0;
  869. }
  870. __initcall(audit_init);
  871. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  872. static int __init audit_enable(char *str)
  873. {
  874. audit_default = !!simple_strtol(str, NULL, 0);
  875. if (!audit_default)
  876. audit_initialized = AUDIT_DISABLED;
  877. printk(KERN_INFO "audit: %s", audit_default ? "enabled" : "disabled");
  878. if (audit_initialized == AUDIT_INITIALIZED) {
  879. audit_enabled = audit_default;
  880. audit_ever_enabled |= !!audit_default;
  881. } else if (audit_initialized == AUDIT_UNINITIALIZED) {
  882. printk(" (after initialization)");
  883. } else {
  884. printk(" (until reboot)");
  885. }
  886. printk("\n");
  887. return 1;
  888. }
  889. __setup("audit=", audit_enable);
  890. static void audit_buffer_free(struct audit_buffer *ab)
  891. {
  892. unsigned long flags;
  893. if (!ab)
  894. return;
  895. if (ab->skb)
  896. kfree_skb(ab->skb);
  897. spin_lock_irqsave(&audit_freelist_lock, flags);
  898. if (audit_freelist_count > AUDIT_MAXFREE)
  899. kfree(ab);
  900. else {
  901. audit_freelist_count++;
  902. list_add(&ab->list, &audit_freelist);
  903. }
  904. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  905. }
  906. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  907. gfp_t gfp_mask, int type)
  908. {
  909. unsigned long flags;
  910. struct audit_buffer *ab = NULL;
  911. struct nlmsghdr *nlh;
  912. spin_lock_irqsave(&audit_freelist_lock, flags);
  913. if (!list_empty(&audit_freelist)) {
  914. ab = list_entry(audit_freelist.next,
  915. struct audit_buffer, list);
  916. list_del(&ab->list);
  917. --audit_freelist_count;
  918. }
  919. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  920. if (!ab) {
  921. ab = kmalloc(sizeof(*ab), gfp_mask);
  922. if (!ab)
  923. goto err;
  924. }
  925. ab->ctx = ctx;
  926. ab->gfp_mask = gfp_mask;
  927. ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
  928. if (!ab->skb)
  929. goto nlmsg_failure;
  930. nlh = NLMSG_NEW(ab->skb, 0, 0, type, 0, 0);
  931. return ab;
  932. nlmsg_failure: /* Used by NLMSG_NEW */
  933. kfree_skb(ab->skb);
  934. ab->skb = NULL;
  935. err:
  936. audit_buffer_free(ab);
  937. return NULL;
  938. }
  939. /**
  940. * audit_serial - compute a serial number for the audit record
  941. *
  942. * Compute a serial number for the audit record. Audit records are
  943. * written to user-space as soon as they are generated, so a complete
  944. * audit record may be written in several pieces. The timestamp of the
  945. * record and this serial number are used by the user-space tools to
  946. * determine which pieces belong to the same audit record. The
  947. * (timestamp,serial) tuple is unique for each syscall and is live from
  948. * syscall entry to syscall exit.
  949. *
  950. * NOTE: Another possibility is to store the formatted records off the
  951. * audit context (for those records that have a context), and emit them
  952. * all at syscall exit. However, this could delay the reporting of
  953. * significant errors until syscall exit (or never, if the system
  954. * halts).
  955. */
  956. unsigned int audit_serial(void)
  957. {
  958. static DEFINE_SPINLOCK(serial_lock);
  959. static unsigned int serial = 0;
  960. unsigned long flags;
  961. unsigned int ret;
  962. spin_lock_irqsave(&serial_lock, flags);
  963. do {
  964. ret = ++serial;
  965. } while (unlikely(!ret));
  966. spin_unlock_irqrestore(&serial_lock, flags);
  967. return ret;
  968. }
  969. static inline void audit_get_stamp(struct audit_context *ctx,
  970. struct timespec *t, unsigned int *serial)
  971. {
  972. if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
  973. *t = CURRENT_TIME;
  974. *serial = audit_serial();
  975. }
  976. }
  977. /* Obtain an audit buffer. This routine does locking to obtain the
  978. * audit buffer, but then no locking is required for calls to
  979. * audit_log_*format. If the tsk is a task that is currently in a
  980. * syscall, then the syscall is marked as auditable and an audit record
  981. * will be written at syscall exit. If there is no associated task, tsk
  982. * should be NULL. */
  983. /**
  984. * audit_log_start - obtain an audit buffer
  985. * @ctx: audit_context (may be NULL)
  986. * @gfp_mask: type of allocation
  987. * @type: audit message type
  988. *
  989. * Returns audit_buffer pointer on success or NULL on error.
  990. *
  991. * Obtain an audit buffer. This routine does locking to obtain the
  992. * audit buffer, but then no locking is required for calls to
  993. * audit_log_*format. If the task (ctx) is a task that is currently in a
  994. * syscall, then the syscall is marked as auditable and an audit record
  995. * will be written at syscall exit. If there is no associated task, then
  996. * task context (ctx) should be NULL.
  997. */
  998. struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
  999. int type)
  1000. {
  1001. struct audit_buffer *ab = NULL;
  1002. struct timespec t;
  1003. unsigned int uninitialized_var(serial);
  1004. int reserve;
  1005. unsigned long timeout_start = jiffies;
  1006. if (audit_initialized != AUDIT_INITIALIZED)
  1007. return NULL;
  1008. if (unlikely(audit_filter_type(type)))
  1009. return NULL;
  1010. if (gfp_mask & __GFP_WAIT)
  1011. reserve = 0;
  1012. else
  1013. reserve = 5; /* Allow atomic callers to go up to five
  1014. entries over the normal backlog limit */
  1015. while (audit_backlog_limit
  1016. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
  1017. if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
  1018. && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
  1019. /* Wait for auditd to drain the queue a little */
  1020. DECLARE_WAITQUEUE(wait, current);
  1021. set_current_state(TASK_INTERRUPTIBLE);
  1022. add_wait_queue(&audit_backlog_wait, &wait);
  1023. if (audit_backlog_limit &&
  1024. skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
  1025. schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
  1026. __set_current_state(TASK_RUNNING);
  1027. remove_wait_queue(&audit_backlog_wait, &wait);
  1028. continue;
  1029. }
  1030. if (audit_rate_check() && printk_ratelimit())
  1031. printk(KERN_WARNING
  1032. "audit: audit_backlog=%d > "
  1033. "audit_backlog_limit=%d\n",
  1034. skb_queue_len(&audit_skb_queue),
  1035. audit_backlog_limit);
  1036. audit_log_lost("backlog limit exceeded");
  1037. audit_backlog_wait_time = audit_backlog_wait_overflow;
  1038. wake_up(&audit_backlog_wait);
  1039. return NULL;
  1040. }
  1041. ab = audit_buffer_alloc(ctx, gfp_mask, type);
  1042. if (!ab) {
  1043. audit_log_lost("out of memory in audit_log_start");
  1044. return NULL;
  1045. }
  1046. audit_get_stamp(ab->ctx, &t, &serial);
  1047. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  1048. t.tv_sec, t.tv_nsec/1000000, serial);
  1049. return ab;
  1050. }
  1051. /**
  1052. * audit_expand - expand skb in the audit buffer
  1053. * @ab: audit_buffer
  1054. * @extra: space to add at tail of the skb
  1055. *
  1056. * Returns 0 (no space) on failed expansion, or available space if
  1057. * successful.
  1058. */
  1059. static inline int audit_expand(struct audit_buffer *ab, int extra)
  1060. {
  1061. struct sk_buff *skb = ab->skb;
  1062. int oldtail = skb_tailroom(skb);
  1063. int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
  1064. int newtail = skb_tailroom(skb);
  1065. if (ret < 0) {
  1066. audit_log_lost("out of memory in audit_expand");
  1067. return 0;
  1068. }
  1069. skb->truesize += newtail - oldtail;
  1070. return newtail;
  1071. }
  1072. /*
  1073. * Format an audit message into the audit buffer. If there isn't enough
  1074. * room in the audit buffer, more room will be allocated and vsnprint
  1075. * will be called a second time. Currently, we assume that a printk
  1076. * can't format message larger than 1024 bytes, so we don't either.
  1077. */
  1078. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  1079. va_list args)
  1080. {
  1081. int len, avail;
  1082. struct sk_buff *skb;
  1083. va_list args2;
  1084. if (!ab)
  1085. return;
  1086. BUG_ON(!ab->skb);
  1087. skb = ab->skb;
  1088. avail = skb_tailroom(skb);
  1089. if (avail == 0) {
  1090. avail = audit_expand(ab, AUDIT_BUFSIZ);
  1091. if (!avail)
  1092. goto out;
  1093. }
  1094. va_copy(args2, args);
  1095. len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
  1096. if (len >= avail) {
  1097. /* The printk buffer is 1024 bytes long, so if we get
  1098. * here and AUDIT_BUFSIZ is at least 1024, then we can
  1099. * log everything that printk could have logged. */
  1100. avail = audit_expand(ab,
  1101. max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  1102. if (!avail)
  1103. goto out;
  1104. len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
  1105. }
  1106. va_end(args2);
  1107. if (len > 0)
  1108. skb_put(skb, len);
  1109. out:
  1110. return;
  1111. }
  1112. /**
  1113. * audit_log_format - format a message into the audit buffer.
  1114. * @ab: audit_buffer
  1115. * @fmt: format string
  1116. * @...: optional parameters matching @fmt string
  1117. *
  1118. * All the work is done in audit_log_vformat.
  1119. */
  1120. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  1121. {
  1122. va_list args;
  1123. if (!ab)
  1124. return;
  1125. va_start(args, fmt);
  1126. audit_log_vformat(ab, fmt, args);
  1127. va_end(args);
  1128. }
  1129. /**
  1130. * audit_log_hex - convert a buffer to hex and append it to the audit skb
  1131. * @ab: the audit_buffer
  1132. * @buf: buffer to convert to hex
  1133. * @len: length of @buf to be converted
  1134. *
  1135. * No return value; failure to expand is silently ignored.
  1136. *
  1137. * This function will take the passed buf and convert it into a string of
  1138. * ascii hex digits. The new string is placed onto the skb.
  1139. */
  1140. void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
  1141. size_t len)
  1142. {
  1143. int i, avail, new_len;
  1144. unsigned char *ptr;
  1145. struct sk_buff *skb;
  1146. static const unsigned char *hex = "0123456789ABCDEF";
  1147. if (!ab)
  1148. return;
  1149. BUG_ON(!ab->skb);
  1150. skb = ab->skb;
  1151. avail = skb_tailroom(skb);
  1152. new_len = len<<1;
  1153. if (new_len >= avail) {
  1154. /* Round the buffer request up to the next multiple */
  1155. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  1156. avail = audit_expand(ab, new_len);
  1157. if (!avail)
  1158. return;
  1159. }
  1160. ptr = skb_tail_pointer(skb);
  1161. for (i=0; i<len; i++) {
  1162. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  1163. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  1164. }
  1165. *ptr = 0;
  1166. skb_put(skb, len << 1); /* new string is twice the old string */
  1167. }
  1168. /*
  1169. * Format a string of no more than slen characters into the audit buffer,
  1170. * enclosed in quote marks.
  1171. */
  1172. void audit_log_n_string(struct audit_buffer *ab, const char *string,
  1173. size_t slen)
  1174. {
  1175. int avail, new_len;
  1176. unsigned char *ptr;
  1177. struct sk_buff *skb;
  1178. if (!ab)
  1179. return;
  1180. BUG_ON(!ab->skb);
  1181. skb = ab->skb;
  1182. avail = skb_tailroom(skb);
  1183. new_len = slen + 3; /* enclosing quotes + null terminator */
  1184. if (new_len > avail) {
  1185. avail = audit_expand(ab, new_len);
  1186. if (!avail)
  1187. return;
  1188. }
  1189. ptr = skb_tail_pointer(skb);
  1190. *ptr++ = '"';
  1191. memcpy(ptr, string, slen);
  1192. ptr += slen;
  1193. *ptr++ = '"';
  1194. *ptr = 0;
  1195. skb_put(skb, slen + 2); /* don't include null terminator */
  1196. }
  1197. /**
  1198. * audit_string_contains_control - does a string need to be logged in hex
  1199. * @string: string to be checked
  1200. * @len: max length of the string to check
  1201. */
  1202. int audit_string_contains_control(const char *string, size_t len)
  1203. {
  1204. const unsigned char *p;
  1205. for (p = string; p < (const unsigned char *)string + len; p++) {
  1206. if (*p == '"' || *p < 0x21 || *p > 0x7e)
  1207. return 1;
  1208. }
  1209. return 0;
  1210. }
  1211. /**
  1212. * audit_log_n_untrustedstring - log a string that may contain random characters
  1213. * @ab: audit_buffer
  1214. * @len: length of string (not including trailing null)
  1215. * @string: string to be logged
  1216. *
  1217. * This code will escape a string that is passed to it if the string
  1218. * contains a control character, unprintable character, double quote mark,
  1219. * or a space. Unescaped strings will start and end with a double quote mark.
  1220. * Strings that are escaped are printed in hex (2 digits per char).
  1221. *
  1222. * The caller specifies the number of characters in the string to log, which may
  1223. * or may not be the entire string.
  1224. */
  1225. void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
  1226. size_t len)
  1227. {
  1228. if (audit_string_contains_control(string, len))
  1229. audit_log_n_hex(ab, string, len);
  1230. else
  1231. audit_log_n_string(ab, string, len);
  1232. }
  1233. /**
  1234. * audit_log_untrustedstring - log a string that may contain random characters
  1235. * @ab: audit_buffer
  1236. * @string: string to be logged
  1237. *
  1238. * Same as audit_log_n_untrustedstring(), except that strlen is used to
  1239. * determine string length.
  1240. */
  1241. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  1242. {
  1243. audit_log_n_untrustedstring(ab, string, strlen(string));
  1244. }
  1245. /* This is a helper-function to print the escaped d_path */
  1246. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  1247. struct path *path)
  1248. {
  1249. char *p, *pathname;
  1250. if (prefix)
  1251. audit_log_format(ab, " %s", prefix);
  1252. /* We will allow 11 spaces for ' (deleted)' to be appended */
  1253. pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
  1254. if (!pathname) {
  1255. audit_log_string(ab, "<no_memory>");
  1256. return;
  1257. }
  1258. p = d_path(path, pathname, PATH_MAX+11);
  1259. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  1260. /* FIXME: can we save some information here? */
  1261. audit_log_string(ab, "<too_long>");
  1262. } else
  1263. audit_log_untrustedstring(ab, p);
  1264. kfree(pathname);
  1265. }
  1266. void audit_log_key(struct audit_buffer *ab, char *key)
  1267. {
  1268. audit_log_format(ab, " key=");
  1269. if (key)
  1270. audit_log_untrustedstring(ab, key);
  1271. else
  1272. audit_log_format(ab, "(null)");
  1273. }
  1274. /**
  1275. * audit_log_end - end one audit record
  1276. * @ab: the audit_buffer
  1277. *
  1278. * The netlink_* functions cannot be called inside an irq context, so
  1279. * the audit buffer is placed on a queue and a tasklet is scheduled to
  1280. * remove them from the queue outside the irq context. May be called in
  1281. * any context.
  1282. */
  1283. void audit_log_end(struct audit_buffer *ab)
  1284. {
  1285. if (!ab)
  1286. return;
  1287. if (!audit_rate_check()) {
  1288. audit_log_lost("rate limit exceeded");
  1289. } else {
  1290. struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
  1291. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  1292. if (audit_pid) {
  1293. skb_queue_tail(&audit_skb_queue, ab->skb);
  1294. wake_up_interruptible(&kauditd_wait);
  1295. } else {
  1296. audit_printk_skb(ab->skb);
  1297. }
  1298. ab->skb = NULL;
  1299. }
  1300. audit_buffer_free(ab);
  1301. }
  1302. /**
  1303. * audit_log - Log an audit record
  1304. * @ctx: audit context
  1305. * @gfp_mask: type of allocation
  1306. * @type: audit message type
  1307. * @fmt: format string to use
  1308. * @...: variable parameters matching the format string
  1309. *
  1310. * This is a convenience function that calls audit_log_start,
  1311. * audit_log_vformat, and audit_log_end. It may be called
  1312. * in any context.
  1313. */
  1314. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  1315. const char *fmt, ...)
  1316. {
  1317. struct audit_buffer *ab;
  1318. va_list args;
  1319. ab = audit_log_start(ctx, gfp_mask, type);
  1320. if (ab) {
  1321. va_start(args, fmt);
  1322. audit_log_vformat(ab, fmt, args);
  1323. va_end(args);
  1324. audit_log_end(ab);
  1325. }
  1326. }
  1327. EXPORT_SYMBOL(audit_log_start);
  1328. EXPORT_SYMBOL(audit_log_end);
  1329. EXPORT_SYMBOL(audit_log_format);
  1330. EXPORT_SYMBOL(audit_log);