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