audit.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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-2004 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 "audit.h"
  57. /* No auditing will take place until audit_initialized != 0.
  58. * (Initialization happens after skb_init is called.) */
  59. static int audit_initialized;
  60. /* No syscall auditing will take place unless audit_enabled != 0. */
  61. int audit_enabled;
  62. /* Default state when kernel boots without any parameters. */
  63. static int audit_default;
  64. /* If auditing cannot proceed, audit_failure selects what happens. */
  65. static int audit_failure = AUDIT_FAIL_PRINTK;
  66. /* If audit records are to be written to the netlink socket, audit_pid
  67. * contains the (non-zero) pid. */
  68. int audit_pid;
  69. /* If audit_rate_limit is non-zero, limit the rate of sending audit records
  70. * to that number per second. This prevents DoS attacks, but results in
  71. * audit records being dropped. */
  72. static int audit_rate_limit;
  73. /* Number of outstanding audit_buffers allowed. */
  74. static int audit_backlog_limit = 64;
  75. static int audit_backlog_wait_time = 60 * HZ;
  76. static int audit_backlog_wait_overflow = 0;
  77. /* The identity of the user shutting down the audit system. */
  78. uid_t audit_sig_uid = -1;
  79. pid_t audit_sig_pid = -1;
  80. /* Records can be lost in several ways:
  81. 0) [suppressed in audit_alloc]
  82. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  83. 2) out of memory in audit_log_move [alloc_skb]
  84. 3) suppressed due to audit_rate_limit
  85. 4) suppressed due to audit_backlog_limit
  86. */
  87. static atomic_t audit_lost = ATOMIC_INIT(0);
  88. /* The netlink socket. */
  89. static struct sock *audit_sock;
  90. /* The audit_freelist is a list of pre-allocated audit buffers (if more
  91. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  92. * being placed on the freelist). */
  93. static DEFINE_SPINLOCK(audit_freelist_lock);
  94. static int audit_freelist_count;
  95. static LIST_HEAD(audit_freelist);
  96. static struct sk_buff_head audit_skb_queue;
  97. static struct task_struct *kauditd_task;
  98. static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
  99. static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
  100. /* The netlink socket is only to be read by 1 CPU, which lets us assume
  101. * that list additions and deletions never happen simultaneously in
  102. * auditsc.c */
  103. DEFINE_MUTEX(audit_netlink_mutex);
  104. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  105. * audit records. Since printk uses a 1024 byte buffer, this buffer
  106. * should be at least that large. */
  107. #define AUDIT_BUFSIZ 1024
  108. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  109. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  110. #define AUDIT_MAXFREE (2*NR_CPUS)
  111. /* The audit_buffer is used when formatting an audit record. The caller
  112. * locks briefly to get the record off the freelist or to allocate the
  113. * buffer, and locks briefly to send the buffer to the netlink layer or
  114. * to place it on a transmit queue. Multiple audit_buffers can be in
  115. * use simultaneously. */
  116. struct audit_buffer {
  117. struct list_head list;
  118. struct sk_buff *skb; /* formatted skb ready to send */
  119. struct audit_context *ctx; /* NULL or associated context */
  120. gfp_t gfp_mask;
  121. };
  122. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  123. {
  124. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  125. nlh->nlmsg_pid = pid;
  126. }
  127. void audit_panic(const char *message)
  128. {
  129. switch (audit_failure)
  130. {
  131. case AUDIT_FAIL_SILENT:
  132. break;
  133. case AUDIT_FAIL_PRINTK:
  134. printk(KERN_ERR "audit: %s\n", message);
  135. break;
  136. case AUDIT_FAIL_PANIC:
  137. panic("audit: %s\n", message);
  138. break;
  139. }
  140. }
  141. static inline int audit_rate_check(void)
  142. {
  143. static unsigned long last_check = 0;
  144. static int messages = 0;
  145. static DEFINE_SPINLOCK(lock);
  146. unsigned long flags;
  147. unsigned long now;
  148. unsigned long elapsed;
  149. int retval = 0;
  150. if (!audit_rate_limit) return 1;
  151. spin_lock_irqsave(&lock, flags);
  152. if (++messages < audit_rate_limit) {
  153. retval = 1;
  154. } else {
  155. now = jiffies;
  156. elapsed = now - last_check;
  157. if (elapsed > HZ) {
  158. last_check = now;
  159. messages = 0;
  160. retval = 1;
  161. }
  162. }
  163. spin_unlock_irqrestore(&lock, flags);
  164. return retval;
  165. }
  166. /**
  167. * audit_log_lost - conditionally log lost audit message event
  168. * @message: the message stating reason for lost audit message
  169. *
  170. * Emit at least 1 message per second, even if audit_rate_check is
  171. * throttling.
  172. * Always increment the lost messages counter.
  173. */
  174. void audit_log_lost(const char *message)
  175. {
  176. static unsigned long last_msg = 0;
  177. static DEFINE_SPINLOCK(lock);
  178. unsigned long flags;
  179. unsigned long now;
  180. int print;
  181. atomic_inc(&audit_lost);
  182. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  183. if (!print) {
  184. spin_lock_irqsave(&lock, flags);
  185. now = jiffies;
  186. if (now - last_msg > HZ) {
  187. print = 1;
  188. last_msg = now;
  189. }
  190. spin_unlock_irqrestore(&lock, flags);
  191. }
  192. if (print) {
  193. printk(KERN_WARNING
  194. "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
  195. atomic_read(&audit_lost),
  196. audit_rate_limit,
  197. audit_backlog_limit);
  198. audit_panic(message);
  199. }
  200. }
  201. static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
  202. {
  203. int old = audit_rate_limit;
  204. if (sid) {
  205. char *ctx = NULL;
  206. u32 len;
  207. int rc;
  208. if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
  209. return rc;
  210. else
  211. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  212. "audit_rate_limit=%d old=%d by auid=%u subj=%s",
  213. limit, old, loginuid, ctx);
  214. kfree(ctx);
  215. } else
  216. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  217. "audit_rate_limit=%d old=%d by auid=%u",
  218. limit, old, loginuid);
  219. audit_rate_limit = limit;
  220. return old;
  221. }
  222. static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
  223. {
  224. int old = audit_backlog_limit;
  225. if (sid) {
  226. char *ctx = NULL;
  227. u32 len;
  228. int rc;
  229. if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
  230. return rc;
  231. else
  232. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  233. "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
  234. limit, old, loginuid, ctx);
  235. kfree(ctx);
  236. } else
  237. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  238. "audit_backlog_limit=%d old=%d by auid=%u",
  239. limit, old, loginuid);
  240. audit_backlog_limit = limit;
  241. return old;
  242. }
  243. static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
  244. {
  245. int old = audit_enabled;
  246. if (state != 0 && state != 1)
  247. return -EINVAL;
  248. if (sid) {
  249. char *ctx = NULL;
  250. u32 len;
  251. int rc;
  252. if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
  253. return rc;
  254. else
  255. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  256. "audit_enabled=%d old=%d by auid=%u subj=%s",
  257. state, old, loginuid, ctx);
  258. kfree(ctx);
  259. } else
  260. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  261. "audit_enabled=%d old=%d by auid=%u",
  262. state, old, loginuid);
  263. audit_enabled = state;
  264. return old;
  265. }
  266. static int audit_set_failure(int state, uid_t loginuid, u32 sid)
  267. {
  268. int old = audit_failure;
  269. if (state != AUDIT_FAIL_SILENT
  270. && state != AUDIT_FAIL_PRINTK
  271. && state != AUDIT_FAIL_PANIC)
  272. return -EINVAL;
  273. if (sid) {
  274. char *ctx = NULL;
  275. u32 len;
  276. int rc;
  277. if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
  278. return rc;
  279. else
  280. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  281. "audit_failure=%d old=%d by auid=%u subj=%s",
  282. state, old, loginuid, ctx);
  283. kfree(ctx);
  284. } else
  285. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  286. "audit_failure=%d old=%d by auid=%u",
  287. state, old, loginuid);
  288. audit_failure = state;
  289. return old;
  290. }
  291. static int kauditd_thread(void *dummy)
  292. {
  293. struct sk_buff *skb;
  294. while (1) {
  295. skb = skb_dequeue(&audit_skb_queue);
  296. wake_up(&audit_backlog_wait);
  297. if (skb) {
  298. if (audit_pid) {
  299. int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
  300. if (err < 0) {
  301. BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
  302. printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
  303. audit_pid = 0;
  304. }
  305. } else {
  306. printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
  307. kfree_skb(skb);
  308. }
  309. } else {
  310. DECLARE_WAITQUEUE(wait, current);
  311. set_current_state(TASK_INTERRUPTIBLE);
  312. add_wait_queue(&kauditd_wait, &wait);
  313. if (!skb_queue_len(&audit_skb_queue)) {
  314. try_to_freeze();
  315. schedule();
  316. }
  317. __set_current_state(TASK_RUNNING);
  318. remove_wait_queue(&kauditd_wait, &wait);
  319. }
  320. }
  321. return 0;
  322. }
  323. /**
  324. * audit_send_reply - send an audit reply message via netlink
  325. * @pid: process id to send reply to
  326. * @seq: sequence number
  327. * @type: audit message type
  328. * @done: done (last) flag
  329. * @multi: multi-part message flag
  330. * @payload: payload data
  331. * @size: payload size
  332. *
  333. * Allocates an skb, builds the netlink message, and sends it to the pid.
  334. * No failure notifications.
  335. */
  336. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  337. void *payload, int size)
  338. {
  339. struct sk_buff *skb;
  340. struct nlmsghdr *nlh;
  341. int len = NLMSG_SPACE(size);
  342. void *data;
  343. int flags = multi ? NLM_F_MULTI : 0;
  344. int t = done ? NLMSG_DONE : type;
  345. skb = alloc_skb(len, GFP_KERNEL);
  346. if (!skb)
  347. return;
  348. nlh = NLMSG_PUT(skb, pid, seq, t, size);
  349. nlh->nlmsg_flags = flags;
  350. data = NLMSG_DATA(nlh);
  351. memcpy(data, payload, size);
  352. /* Ignore failure. It'll only happen if the sender goes away,
  353. because our timeout is set to infinite. */
  354. netlink_unicast(audit_sock, skb, pid, 0);
  355. return;
  356. nlmsg_failure: /* Used by NLMSG_PUT */
  357. if (skb)
  358. kfree_skb(skb);
  359. }
  360. /*
  361. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  362. * control messages.
  363. */
  364. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  365. {
  366. int err = 0;
  367. switch (msg_type) {
  368. case AUDIT_GET:
  369. case AUDIT_LIST:
  370. case AUDIT_LIST_RULES:
  371. case AUDIT_SET:
  372. case AUDIT_ADD:
  373. case AUDIT_ADD_RULE:
  374. case AUDIT_DEL:
  375. case AUDIT_DEL_RULE:
  376. case AUDIT_SIGNAL_INFO:
  377. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  378. err = -EPERM;
  379. break;
  380. case AUDIT_USER:
  381. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  382. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  383. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  384. err = -EPERM;
  385. break;
  386. default: /* bad msg */
  387. err = -EINVAL;
  388. }
  389. return err;
  390. }
  391. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  392. {
  393. u32 uid, pid, seq, sid;
  394. void *data;
  395. struct audit_status *status_get, status_set;
  396. int err;
  397. struct audit_buffer *ab;
  398. u16 msg_type = nlh->nlmsg_type;
  399. uid_t loginuid; /* loginuid of sender */
  400. struct audit_sig_info sig_data;
  401. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  402. if (err)
  403. return err;
  404. /* As soon as there's any sign of userspace auditd,
  405. * start kauditd to talk to it */
  406. if (!kauditd_task)
  407. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  408. if (IS_ERR(kauditd_task)) {
  409. err = PTR_ERR(kauditd_task);
  410. kauditd_task = NULL;
  411. return err;
  412. }
  413. pid = NETLINK_CREDS(skb)->pid;
  414. uid = NETLINK_CREDS(skb)->uid;
  415. loginuid = NETLINK_CB(skb).loginuid;
  416. sid = NETLINK_CB(skb).sid;
  417. seq = nlh->nlmsg_seq;
  418. data = NLMSG_DATA(nlh);
  419. switch (msg_type) {
  420. case AUDIT_GET:
  421. status_set.enabled = audit_enabled;
  422. status_set.failure = audit_failure;
  423. status_set.pid = audit_pid;
  424. status_set.rate_limit = audit_rate_limit;
  425. status_set.backlog_limit = audit_backlog_limit;
  426. status_set.lost = atomic_read(&audit_lost);
  427. status_set.backlog = skb_queue_len(&audit_skb_queue);
  428. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  429. &status_set, sizeof(status_set));
  430. break;
  431. case AUDIT_SET:
  432. if (nlh->nlmsg_len < sizeof(struct audit_status))
  433. return -EINVAL;
  434. status_get = (struct audit_status *)data;
  435. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  436. err = audit_set_enabled(status_get->enabled,
  437. loginuid, sid);
  438. if (err < 0) return err;
  439. }
  440. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  441. err = audit_set_failure(status_get->failure,
  442. loginuid, sid);
  443. if (err < 0) return err;
  444. }
  445. if (status_get->mask & AUDIT_STATUS_PID) {
  446. int old = audit_pid;
  447. if (sid) {
  448. char *ctx = NULL;
  449. u32 len;
  450. int rc;
  451. if ((rc = selinux_ctxid_to_string(
  452. sid, &ctx, &len)))
  453. return rc;
  454. else
  455. audit_log(NULL, GFP_KERNEL,
  456. AUDIT_CONFIG_CHANGE,
  457. "audit_pid=%d old=%d by auid=%u subj=%s",
  458. status_get->pid, old,
  459. loginuid, ctx);
  460. kfree(ctx);
  461. } else
  462. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  463. "audit_pid=%d old=%d by auid=%u",
  464. status_get->pid, old, loginuid);
  465. audit_pid = status_get->pid;
  466. }
  467. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  468. audit_set_rate_limit(status_get->rate_limit,
  469. loginuid, sid);
  470. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  471. audit_set_backlog_limit(status_get->backlog_limit,
  472. loginuid, sid);
  473. break;
  474. case AUDIT_USER:
  475. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  476. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  477. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  478. return 0;
  479. err = audit_filter_user(&NETLINK_CB(skb), msg_type);
  480. if (err == 1) {
  481. err = 0;
  482. ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  483. if (ab) {
  484. audit_log_format(ab,
  485. "user pid=%d uid=%u auid=%u",
  486. pid, uid, loginuid);
  487. if (sid) {
  488. char *ctx = NULL;
  489. u32 len;
  490. if (selinux_ctxid_to_string(
  491. sid, &ctx, &len)) {
  492. audit_log_format(ab,
  493. " ssid=%u", sid);
  494. /* Maybe call audit_panic? */
  495. } else
  496. audit_log_format(ab,
  497. " subj=%s", ctx);
  498. kfree(ctx);
  499. }
  500. audit_log_format(ab, " msg='%.1024s'",
  501. (char *)data);
  502. audit_set_pid(ab, pid);
  503. audit_log_end(ab);
  504. }
  505. }
  506. break;
  507. case AUDIT_ADD:
  508. case AUDIT_DEL:
  509. if (nlmsg_len(nlh) < sizeof(struct audit_rule))
  510. return -EINVAL;
  511. /* fallthrough */
  512. case AUDIT_LIST:
  513. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  514. uid, seq, data, nlmsg_len(nlh),
  515. loginuid, sid);
  516. break;
  517. case AUDIT_ADD_RULE:
  518. case AUDIT_DEL_RULE:
  519. if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
  520. return -EINVAL;
  521. /* fallthrough */
  522. case AUDIT_LIST_RULES:
  523. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  524. uid, seq, data, nlmsg_len(nlh),
  525. loginuid, sid);
  526. break;
  527. case AUDIT_SIGNAL_INFO:
  528. sig_data.uid = audit_sig_uid;
  529. sig_data.pid = audit_sig_pid;
  530. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  531. 0, 0, &sig_data, sizeof(sig_data));
  532. break;
  533. default:
  534. err = -EINVAL;
  535. break;
  536. }
  537. return err < 0 ? err : 0;
  538. }
  539. /*
  540. * Get message from skb (based on rtnetlink_rcv_skb). Each message is
  541. * processed by audit_receive_msg. Malformed skbs with wrong length are
  542. * discarded silently.
  543. */
  544. static void audit_receive_skb(struct sk_buff *skb)
  545. {
  546. int err;
  547. struct nlmsghdr *nlh;
  548. u32 rlen;
  549. while (skb->len >= NLMSG_SPACE(0)) {
  550. nlh = (struct nlmsghdr *)skb->data;
  551. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  552. return;
  553. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  554. if (rlen > skb->len)
  555. rlen = skb->len;
  556. if ((err = audit_receive_msg(skb, nlh))) {
  557. netlink_ack(skb, nlh, err);
  558. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  559. netlink_ack(skb, nlh, 0);
  560. skb_pull(skb, rlen);
  561. }
  562. }
  563. /* Receive messages from netlink socket. */
  564. static void audit_receive(struct sock *sk, int length)
  565. {
  566. struct sk_buff *skb;
  567. unsigned int qlen;
  568. mutex_lock(&audit_netlink_mutex);
  569. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  570. skb = skb_dequeue(&sk->sk_receive_queue);
  571. audit_receive_skb(skb);
  572. kfree_skb(skb);
  573. }
  574. mutex_unlock(&audit_netlink_mutex);
  575. }
  576. /* Initialize audit support at boot time. */
  577. static int __init audit_init(void)
  578. {
  579. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  580. audit_default ? "enabled" : "disabled");
  581. audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
  582. THIS_MODULE);
  583. if (!audit_sock)
  584. audit_panic("cannot initialize netlink socket");
  585. else
  586. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  587. skb_queue_head_init(&audit_skb_queue);
  588. audit_initialized = 1;
  589. audit_enabled = audit_default;
  590. /* Register the callback with selinux. This callback will be invoked
  591. * when a new policy is loaded. */
  592. selinux_audit_set_callback(&selinux_audit_rule_update);
  593. audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
  594. return 0;
  595. }
  596. __initcall(audit_init);
  597. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  598. static int __init audit_enable(char *str)
  599. {
  600. audit_default = !!simple_strtol(str, NULL, 0);
  601. printk(KERN_INFO "audit: %s%s\n",
  602. audit_default ? "enabled" : "disabled",
  603. audit_initialized ? "" : " (after initialization)");
  604. if (audit_initialized)
  605. audit_enabled = audit_default;
  606. return 1;
  607. }
  608. __setup("audit=", audit_enable);
  609. static void audit_buffer_free(struct audit_buffer *ab)
  610. {
  611. unsigned long flags;
  612. if (!ab)
  613. return;
  614. if (ab->skb)
  615. kfree_skb(ab->skb);
  616. spin_lock_irqsave(&audit_freelist_lock, flags);
  617. if (++audit_freelist_count > AUDIT_MAXFREE)
  618. kfree(ab);
  619. else
  620. list_add(&ab->list, &audit_freelist);
  621. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  622. }
  623. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  624. gfp_t gfp_mask, int type)
  625. {
  626. unsigned long flags;
  627. struct audit_buffer *ab = NULL;
  628. struct nlmsghdr *nlh;
  629. spin_lock_irqsave(&audit_freelist_lock, flags);
  630. if (!list_empty(&audit_freelist)) {
  631. ab = list_entry(audit_freelist.next,
  632. struct audit_buffer, list);
  633. list_del(&ab->list);
  634. --audit_freelist_count;
  635. }
  636. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  637. if (!ab) {
  638. ab = kmalloc(sizeof(*ab), gfp_mask);
  639. if (!ab)
  640. goto err;
  641. }
  642. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  643. if (!ab->skb)
  644. goto err;
  645. ab->ctx = ctx;
  646. ab->gfp_mask = gfp_mask;
  647. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  648. nlh->nlmsg_type = type;
  649. nlh->nlmsg_flags = 0;
  650. nlh->nlmsg_pid = 0;
  651. nlh->nlmsg_seq = 0;
  652. return ab;
  653. err:
  654. audit_buffer_free(ab);
  655. return NULL;
  656. }
  657. /**
  658. * audit_serial - compute a serial number for the audit record
  659. *
  660. * Compute a serial number for the audit record. Audit records are
  661. * written to user-space as soon as they are generated, so a complete
  662. * audit record may be written in several pieces. The timestamp of the
  663. * record and this serial number are used by the user-space tools to
  664. * determine which pieces belong to the same audit record. The
  665. * (timestamp,serial) tuple is unique for each syscall and is live from
  666. * syscall entry to syscall exit.
  667. *
  668. * NOTE: Another possibility is to store the formatted records off the
  669. * audit context (for those records that have a context), and emit them
  670. * all at syscall exit. However, this could delay the reporting of
  671. * significant errors until syscall exit (or never, if the system
  672. * halts).
  673. */
  674. unsigned int audit_serial(void)
  675. {
  676. static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
  677. static unsigned int serial = 0;
  678. unsigned long flags;
  679. unsigned int ret;
  680. spin_lock_irqsave(&serial_lock, flags);
  681. do {
  682. ret = ++serial;
  683. } while (unlikely(!ret));
  684. spin_unlock_irqrestore(&serial_lock, flags);
  685. return ret;
  686. }
  687. static inline void audit_get_stamp(struct audit_context *ctx,
  688. struct timespec *t, unsigned int *serial)
  689. {
  690. if (ctx)
  691. auditsc_get_stamp(ctx, t, serial);
  692. else {
  693. *t = CURRENT_TIME;
  694. *serial = audit_serial();
  695. }
  696. }
  697. /* Obtain an audit buffer. This routine does locking to obtain the
  698. * audit buffer, but then no locking is required for calls to
  699. * audit_log_*format. If the tsk is a task that is currently in a
  700. * syscall, then the syscall is marked as auditable and an audit record
  701. * will be written at syscall exit. If there is no associated task, tsk
  702. * should be NULL. */
  703. /**
  704. * audit_log_start - obtain an audit buffer
  705. * @ctx: audit_context (may be NULL)
  706. * @gfp_mask: type of allocation
  707. * @type: audit message type
  708. *
  709. * Returns audit_buffer pointer on success or NULL on error.
  710. *
  711. * Obtain an audit buffer. This routine does locking to obtain the
  712. * audit buffer, but then no locking is required for calls to
  713. * audit_log_*format. If the task (ctx) is a task that is currently in a
  714. * syscall, then the syscall is marked as auditable and an audit record
  715. * will be written at syscall exit. If there is no associated task, then
  716. * task context (ctx) should be NULL.
  717. */
  718. struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
  719. int type)
  720. {
  721. struct audit_buffer *ab = NULL;
  722. struct timespec t;
  723. unsigned int serial;
  724. int reserve;
  725. unsigned long timeout_start = jiffies;
  726. if (!audit_initialized)
  727. return NULL;
  728. if (unlikely(audit_filter_type(type)))
  729. return NULL;
  730. if (gfp_mask & __GFP_WAIT)
  731. reserve = 0;
  732. else
  733. reserve = 5; /* Allow atomic callers to go up to five
  734. entries over the normal backlog limit */
  735. while (audit_backlog_limit
  736. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
  737. if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
  738. && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
  739. /* Wait for auditd to drain the queue a little */
  740. DECLARE_WAITQUEUE(wait, current);
  741. set_current_state(TASK_INTERRUPTIBLE);
  742. add_wait_queue(&audit_backlog_wait, &wait);
  743. if (audit_backlog_limit &&
  744. skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
  745. schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
  746. __set_current_state(TASK_RUNNING);
  747. remove_wait_queue(&audit_backlog_wait, &wait);
  748. continue;
  749. }
  750. if (audit_rate_check())
  751. printk(KERN_WARNING
  752. "audit: audit_backlog=%d > "
  753. "audit_backlog_limit=%d\n",
  754. skb_queue_len(&audit_skb_queue),
  755. audit_backlog_limit);
  756. audit_log_lost("backlog limit exceeded");
  757. audit_backlog_wait_time = audit_backlog_wait_overflow;
  758. wake_up(&audit_backlog_wait);
  759. return NULL;
  760. }
  761. ab = audit_buffer_alloc(ctx, gfp_mask, type);
  762. if (!ab) {
  763. audit_log_lost("out of memory in audit_log_start");
  764. return NULL;
  765. }
  766. audit_get_stamp(ab->ctx, &t, &serial);
  767. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  768. t.tv_sec, t.tv_nsec/1000000, serial);
  769. return ab;
  770. }
  771. /**
  772. * audit_expand - expand skb in the audit buffer
  773. * @ab: audit_buffer
  774. * @extra: space to add at tail of the skb
  775. *
  776. * Returns 0 (no space) on failed expansion, or available space if
  777. * successful.
  778. */
  779. static inline int audit_expand(struct audit_buffer *ab, int extra)
  780. {
  781. struct sk_buff *skb = ab->skb;
  782. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  783. ab->gfp_mask);
  784. if (ret < 0) {
  785. audit_log_lost("out of memory in audit_expand");
  786. return 0;
  787. }
  788. return skb_tailroom(skb);
  789. }
  790. /*
  791. * Format an audit message into the audit buffer. If there isn't enough
  792. * room in the audit buffer, more room will be allocated and vsnprint
  793. * will be called a second time. Currently, we assume that a printk
  794. * can't format message larger than 1024 bytes, so we don't either.
  795. */
  796. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  797. va_list args)
  798. {
  799. int len, avail;
  800. struct sk_buff *skb;
  801. va_list args2;
  802. if (!ab)
  803. return;
  804. BUG_ON(!ab->skb);
  805. skb = ab->skb;
  806. avail = skb_tailroom(skb);
  807. if (avail == 0) {
  808. avail = audit_expand(ab, AUDIT_BUFSIZ);
  809. if (!avail)
  810. goto out;
  811. }
  812. va_copy(args2, args);
  813. len = vsnprintf(skb->tail, avail, fmt, args);
  814. if (len >= avail) {
  815. /* The printk buffer is 1024 bytes long, so if we get
  816. * here and AUDIT_BUFSIZ is at least 1024, then we can
  817. * log everything that printk could have logged. */
  818. avail = audit_expand(ab,
  819. max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  820. if (!avail)
  821. goto out;
  822. len = vsnprintf(skb->tail, avail, fmt, args2);
  823. }
  824. if (len > 0)
  825. skb_put(skb, len);
  826. out:
  827. return;
  828. }
  829. /**
  830. * audit_log_format - format a message into the audit buffer.
  831. * @ab: audit_buffer
  832. * @fmt: format string
  833. * @...: optional parameters matching @fmt string
  834. *
  835. * All the work is done in audit_log_vformat.
  836. */
  837. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  838. {
  839. va_list args;
  840. if (!ab)
  841. return;
  842. va_start(args, fmt);
  843. audit_log_vformat(ab, fmt, args);
  844. va_end(args);
  845. }
  846. /**
  847. * audit_log_hex - convert a buffer to hex and append it to the audit skb
  848. * @ab: the audit_buffer
  849. * @buf: buffer to convert to hex
  850. * @len: length of @buf to be converted
  851. *
  852. * No return value; failure to expand is silently ignored.
  853. *
  854. * This function will take the passed buf and convert it into a string of
  855. * ascii hex digits. The new string is placed onto the skb.
  856. */
  857. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  858. size_t len)
  859. {
  860. int i, avail, new_len;
  861. unsigned char *ptr;
  862. struct sk_buff *skb;
  863. static const unsigned char *hex = "0123456789ABCDEF";
  864. BUG_ON(!ab->skb);
  865. skb = ab->skb;
  866. avail = skb_tailroom(skb);
  867. new_len = len<<1;
  868. if (new_len >= avail) {
  869. /* Round the buffer request up to the next multiple */
  870. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  871. avail = audit_expand(ab, new_len);
  872. if (!avail)
  873. return;
  874. }
  875. ptr = skb->tail;
  876. for (i=0; i<len; i++) {
  877. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  878. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  879. }
  880. *ptr = 0;
  881. skb_put(skb, len << 1); /* new string is twice the old string */
  882. }
  883. /**
  884. * audit_log_unstrustedstring - log a string that may contain random characters
  885. * @ab: audit_buffer
  886. * @string: string to be logged
  887. *
  888. * This code will escape a string that is passed to it if the string
  889. * contains a control character, unprintable character, double quote mark,
  890. * or a space. Unescaped strings will start and end with a double quote mark.
  891. * Strings that are escaped are printed in hex (2 digits per char).
  892. */
  893. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  894. {
  895. const unsigned char *p = string;
  896. while (*p) {
  897. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  898. audit_log_hex(ab, string, strlen(string));
  899. return;
  900. }
  901. p++;
  902. }
  903. audit_log_format(ab, "\"%s\"", string);
  904. }
  905. /* This is a helper-function to print the escaped d_path */
  906. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  907. struct dentry *dentry, struct vfsmount *vfsmnt)
  908. {
  909. char *p, *path;
  910. if (prefix)
  911. audit_log_format(ab, " %s", prefix);
  912. /* We will allow 11 spaces for ' (deleted)' to be appended */
  913. path = kmalloc(PATH_MAX+11, ab->gfp_mask);
  914. if (!path) {
  915. audit_log_format(ab, "<no memory>");
  916. return;
  917. }
  918. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  919. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  920. /* FIXME: can we save some information here? */
  921. audit_log_format(ab, "<too long>");
  922. } else
  923. audit_log_untrustedstring(ab, p);
  924. kfree(path);
  925. }
  926. /**
  927. * audit_log_end - end one audit record
  928. * @ab: the audit_buffer
  929. *
  930. * The netlink_* functions cannot be called inside an irq context, so
  931. * the audit buffer is placed on a queue and a tasklet is scheduled to
  932. * remove them from the queue outside the irq context. May be called in
  933. * any context.
  934. */
  935. void audit_log_end(struct audit_buffer *ab)
  936. {
  937. if (!ab)
  938. return;
  939. if (!audit_rate_check()) {
  940. audit_log_lost("rate limit exceeded");
  941. } else {
  942. if (audit_pid) {
  943. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  944. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  945. skb_queue_tail(&audit_skb_queue, ab->skb);
  946. ab->skb = NULL;
  947. wake_up_interruptible(&kauditd_wait);
  948. } else {
  949. printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
  950. }
  951. }
  952. audit_buffer_free(ab);
  953. }
  954. /**
  955. * audit_log - Log an audit record
  956. * @ctx: audit context
  957. * @gfp_mask: type of allocation
  958. * @type: audit message type
  959. * @fmt: format string to use
  960. * @...: variable parameters matching the format string
  961. *
  962. * This is a convenience function that calls audit_log_start,
  963. * audit_log_vformat, and audit_log_end. It may be called
  964. * in any context.
  965. */
  966. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  967. const char *fmt, ...)
  968. {
  969. struct audit_buffer *ab;
  970. va_list args;
  971. ab = audit_log_start(ctx, gfp_mask, type);
  972. if (ab) {
  973. va_start(args, fmt);
  974. audit_log_vformat(ab, fmt, args);
  975. va_end(args);
  976. audit_log_end(ab);
  977. }
  978. }
  979. EXPORT_SYMBOL(audit_log_start);
  980. EXPORT_SYMBOL(audit_log_end);
  981. EXPORT_SYMBOL(audit_log_format);
  982. EXPORT_SYMBOL(audit_log);