audit.c 39 KB

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