audit.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. int audit_send_list(void *_dest)
  324. {
  325. struct audit_netlink_list *dest = _dest;
  326. int pid = dest->pid;
  327. struct sk_buff *skb;
  328. /* wait for parent to finish and send an ACK */
  329. mutex_lock(&audit_netlink_mutex);
  330. mutex_unlock(&audit_netlink_mutex);
  331. while ((skb = __skb_dequeue(&dest->q)) != NULL)
  332. netlink_unicast(audit_sock, skb, pid, 0);
  333. kfree(dest);
  334. return 0;
  335. }
  336. struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
  337. int multi, 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 NULL;
  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. return skb;
  353. nlmsg_failure: /* Used by NLMSG_PUT */
  354. if (skb)
  355. kfree_skb(skb);
  356. return NULL;
  357. }
  358. /**
  359. * audit_send_reply - send an audit reply message via netlink
  360. * @pid: process id to send reply to
  361. * @seq: sequence number
  362. * @type: audit message type
  363. * @done: done (last) flag
  364. * @multi: multi-part message flag
  365. * @payload: payload data
  366. * @size: payload size
  367. *
  368. * Allocates an skb, builds the netlink message, and sends it to the pid.
  369. * No failure notifications.
  370. */
  371. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  372. void *payload, int size)
  373. {
  374. struct sk_buff *skb;
  375. skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
  376. if (!skb)
  377. return;
  378. /* Ignore failure. It'll only happen if the sender goes away,
  379. because our timeout is set to infinite. */
  380. netlink_unicast(audit_sock, skb, pid, 0);
  381. return;
  382. }
  383. /*
  384. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  385. * control messages.
  386. */
  387. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  388. {
  389. int err = 0;
  390. switch (msg_type) {
  391. case AUDIT_GET:
  392. case AUDIT_LIST:
  393. case AUDIT_LIST_RULES:
  394. case AUDIT_SET:
  395. case AUDIT_ADD:
  396. case AUDIT_ADD_RULE:
  397. case AUDIT_DEL:
  398. case AUDIT_DEL_RULE:
  399. case AUDIT_SIGNAL_INFO:
  400. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  401. err = -EPERM;
  402. break;
  403. case AUDIT_USER:
  404. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  405. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  406. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  407. err = -EPERM;
  408. break;
  409. default: /* bad msg */
  410. err = -EINVAL;
  411. }
  412. return err;
  413. }
  414. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  415. {
  416. u32 uid, pid, seq, sid;
  417. void *data;
  418. struct audit_status *status_get, status_set;
  419. int err;
  420. struct audit_buffer *ab;
  421. u16 msg_type = nlh->nlmsg_type;
  422. uid_t loginuid; /* loginuid of sender */
  423. struct audit_sig_info sig_data;
  424. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  425. if (err)
  426. return err;
  427. /* As soon as there's any sign of userspace auditd,
  428. * start kauditd to talk to it */
  429. if (!kauditd_task)
  430. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  431. if (IS_ERR(kauditd_task)) {
  432. err = PTR_ERR(kauditd_task);
  433. kauditd_task = NULL;
  434. return err;
  435. }
  436. pid = NETLINK_CREDS(skb)->pid;
  437. uid = NETLINK_CREDS(skb)->uid;
  438. loginuid = NETLINK_CB(skb).loginuid;
  439. sid = NETLINK_CB(skb).sid;
  440. seq = nlh->nlmsg_seq;
  441. data = NLMSG_DATA(nlh);
  442. switch (msg_type) {
  443. case AUDIT_GET:
  444. status_set.enabled = audit_enabled;
  445. status_set.failure = audit_failure;
  446. status_set.pid = audit_pid;
  447. status_set.rate_limit = audit_rate_limit;
  448. status_set.backlog_limit = audit_backlog_limit;
  449. status_set.lost = atomic_read(&audit_lost);
  450. status_set.backlog = skb_queue_len(&audit_skb_queue);
  451. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  452. &status_set, sizeof(status_set));
  453. break;
  454. case AUDIT_SET:
  455. if (nlh->nlmsg_len < sizeof(struct audit_status))
  456. return -EINVAL;
  457. status_get = (struct audit_status *)data;
  458. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  459. err = audit_set_enabled(status_get->enabled,
  460. loginuid, sid);
  461. if (err < 0) return err;
  462. }
  463. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  464. err = audit_set_failure(status_get->failure,
  465. loginuid, sid);
  466. if (err < 0) return err;
  467. }
  468. if (status_get->mask & AUDIT_STATUS_PID) {
  469. int old = audit_pid;
  470. if (sid) {
  471. char *ctx = NULL;
  472. u32 len;
  473. int rc;
  474. if ((rc = selinux_ctxid_to_string(
  475. sid, &ctx, &len)))
  476. return rc;
  477. else
  478. audit_log(NULL, GFP_KERNEL,
  479. AUDIT_CONFIG_CHANGE,
  480. "audit_pid=%d old=%d by auid=%u subj=%s",
  481. status_get->pid, old,
  482. loginuid, ctx);
  483. kfree(ctx);
  484. } else
  485. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  486. "audit_pid=%d old=%d by auid=%u",
  487. status_get->pid, old, loginuid);
  488. audit_pid = status_get->pid;
  489. }
  490. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  491. audit_set_rate_limit(status_get->rate_limit,
  492. loginuid, sid);
  493. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  494. audit_set_backlog_limit(status_get->backlog_limit,
  495. loginuid, sid);
  496. break;
  497. case AUDIT_USER:
  498. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  499. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  500. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  501. return 0;
  502. err = audit_filter_user(&NETLINK_CB(skb), msg_type);
  503. if (err == 1) {
  504. err = 0;
  505. ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  506. if (ab) {
  507. audit_log_format(ab,
  508. "user pid=%d uid=%u auid=%u",
  509. pid, uid, loginuid);
  510. if (sid) {
  511. char *ctx = NULL;
  512. u32 len;
  513. if (selinux_ctxid_to_string(
  514. sid, &ctx, &len)) {
  515. audit_log_format(ab,
  516. " ssid=%u", sid);
  517. /* Maybe call audit_panic? */
  518. } else
  519. audit_log_format(ab,
  520. " subj=%s", ctx);
  521. kfree(ctx);
  522. }
  523. audit_log_format(ab, " msg='%.1024s'",
  524. (char *)data);
  525. audit_set_pid(ab, pid);
  526. audit_log_end(ab);
  527. }
  528. }
  529. break;
  530. case AUDIT_ADD:
  531. case AUDIT_DEL:
  532. if (nlmsg_len(nlh) < sizeof(struct audit_rule))
  533. return -EINVAL;
  534. /* fallthrough */
  535. case AUDIT_LIST:
  536. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  537. uid, seq, data, nlmsg_len(nlh),
  538. loginuid, sid);
  539. break;
  540. case AUDIT_ADD_RULE:
  541. case AUDIT_DEL_RULE:
  542. if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
  543. return -EINVAL;
  544. /* fallthrough */
  545. case AUDIT_LIST_RULES:
  546. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  547. uid, seq, data, nlmsg_len(nlh),
  548. loginuid, sid);
  549. break;
  550. case AUDIT_SIGNAL_INFO:
  551. sig_data.uid = audit_sig_uid;
  552. sig_data.pid = audit_sig_pid;
  553. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  554. 0, 0, &sig_data, sizeof(sig_data));
  555. break;
  556. default:
  557. err = -EINVAL;
  558. break;
  559. }
  560. return err < 0 ? err : 0;
  561. }
  562. /*
  563. * Get message from skb (based on rtnetlink_rcv_skb). Each message is
  564. * processed by audit_receive_msg. Malformed skbs with wrong length are
  565. * discarded silently.
  566. */
  567. static void audit_receive_skb(struct sk_buff *skb)
  568. {
  569. int err;
  570. struct nlmsghdr *nlh;
  571. u32 rlen;
  572. while (skb->len >= NLMSG_SPACE(0)) {
  573. nlh = (struct nlmsghdr *)skb->data;
  574. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  575. return;
  576. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  577. if (rlen > skb->len)
  578. rlen = skb->len;
  579. if ((err = audit_receive_msg(skb, nlh))) {
  580. netlink_ack(skb, nlh, err);
  581. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  582. netlink_ack(skb, nlh, 0);
  583. skb_pull(skb, rlen);
  584. }
  585. }
  586. /* Receive messages from netlink socket. */
  587. static void audit_receive(struct sock *sk, int length)
  588. {
  589. struct sk_buff *skb;
  590. unsigned int qlen;
  591. mutex_lock(&audit_netlink_mutex);
  592. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  593. skb = skb_dequeue(&sk->sk_receive_queue);
  594. audit_receive_skb(skb);
  595. kfree_skb(skb);
  596. }
  597. mutex_unlock(&audit_netlink_mutex);
  598. }
  599. /* Initialize audit support at boot time. */
  600. static int __init audit_init(void)
  601. {
  602. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  603. audit_default ? "enabled" : "disabled");
  604. audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
  605. THIS_MODULE);
  606. if (!audit_sock)
  607. audit_panic("cannot initialize netlink socket");
  608. else
  609. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  610. skb_queue_head_init(&audit_skb_queue);
  611. audit_initialized = 1;
  612. audit_enabled = audit_default;
  613. /* Register the callback with selinux. This callback will be invoked
  614. * when a new policy is loaded. */
  615. selinux_audit_set_callback(&selinux_audit_rule_update);
  616. audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
  617. return 0;
  618. }
  619. __initcall(audit_init);
  620. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  621. static int __init audit_enable(char *str)
  622. {
  623. audit_default = !!simple_strtol(str, NULL, 0);
  624. printk(KERN_INFO "audit: %s%s\n",
  625. audit_default ? "enabled" : "disabled",
  626. audit_initialized ? "" : " (after initialization)");
  627. if (audit_initialized)
  628. audit_enabled = audit_default;
  629. return 1;
  630. }
  631. __setup("audit=", audit_enable);
  632. static void audit_buffer_free(struct audit_buffer *ab)
  633. {
  634. unsigned long flags;
  635. if (!ab)
  636. return;
  637. if (ab->skb)
  638. kfree_skb(ab->skb);
  639. spin_lock_irqsave(&audit_freelist_lock, flags);
  640. if (++audit_freelist_count > AUDIT_MAXFREE)
  641. kfree(ab);
  642. else
  643. list_add(&ab->list, &audit_freelist);
  644. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  645. }
  646. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  647. gfp_t gfp_mask, int type)
  648. {
  649. unsigned long flags;
  650. struct audit_buffer *ab = NULL;
  651. struct nlmsghdr *nlh;
  652. spin_lock_irqsave(&audit_freelist_lock, flags);
  653. if (!list_empty(&audit_freelist)) {
  654. ab = list_entry(audit_freelist.next,
  655. struct audit_buffer, list);
  656. list_del(&ab->list);
  657. --audit_freelist_count;
  658. }
  659. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  660. if (!ab) {
  661. ab = kmalloc(sizeof(*ab), gfp_mask);
  662. if (!ab)
  663. goto err;
  664. }
  665. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  666. if (!ab->skb)
  667. goto err;
  668. ab->ctx = ctx;
  669. ab->gfp_mask = gfp_mask;
  670. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  671. nlh->nlmsg_type = type;
  672. nlh->nlmsg_flags = 0;
  673. nlh->nlmsg_pid = 0;
  674. nlh->nlmsg_seq = 0;
  675. return ab;
  676. err:
  677. audit_buffer_free(ab);
  678. return NULL;
  679. }
  680. /**
  681. * audit_serial - compute a serial number for the audit record
  682. *
  683. * Compute a serial number for the audit record. Audit records are
  684. * written to user-space as soon as they are generated, so a complete
  685. * audit record may be written in several pieces. The timestamp of the
  686. * record and this serial number are used by the user-space tools to
  687. * determine which pieces belong to the same audit record. The
  688. * (timestamp,serial) tuple is unique for each syscall and is live from
  689. * syscall entry to syscall exit.
  690. *
  691. * NOTE: Another possibility is to store the formatted records off the
  692. * audit context (for those records that have a context), and emit them
  693. * all at syscall exit. However, this could delay the reporting of
  694. * significant errors until syscall exit (or never, if the system
  695. * halts).
  696. */
  697. unsigned int audit_serial(void)
  698. {
  699. static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
  700. static unsigned int serial = 0;
  701. unsigned long flags;
  702. unsigned int ret;
  703. spin_lock_irqsave(&serial_lock, flags);
  704. do {
  705. ret = ++serial;
  706. } while (unlikely(!ret));
  707. spin_unlock_irqrestore(&serial_lock, flags);
  708. return ret;
  709. }
  710. static inline void audit_get_stamp(struct audit_context *ctx,
  711. struct timespec *t, unsigned int *serial)
  712. {
  713. if (ctx)
  714. auditsc_get_stamp(ctx, t, serial);
  715. else {
  716. *t = CURRENT_TIME;
  717. *serial = audit_serial();
  718. }
  719. }
  720. /* Obtain an audit buffer. This routine does locking to obtain the
  721. * audit buffer, but then no locking is required for calls to
  722. * audit_log_*format. If the tsk is a task that is currently in a
  723. * syscall, then the syscall is marked as auditable and an audit record
  724. * will be written at syscall exit. If there is no associated task, tsk
  725. * should be NULL. */
  726. /**
  727. * audit_log_start - obtain an audit buffer
  728. * @ctx: audit_context (may be NULL)
  729. * @gfp_mask: type of allocation
  730. * @type: audit message type
  731. *
  732. * Returns audit_buffer pointer on success or NULL on error.
  733. *
  734. * Obtain an audit buffer. This routine does locking to obtain the
  735. * audit buffer, but then no locking is required for calls to
  736. * audit_log_*format. If the task (ctx) is a task that is currently in a
  737. * syscall, then the syscall is marked as auditable and an audit record
  738. * will be written at syscall exit. If there is no associated task, then
  739. * task context (ctx) should be NULL.
  740. */
  741. struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
  742. int type)
  743. {
  744. struct audit_buffer *ab = NULL;
  745. struct timespec t;
  746. unsigned int serial;
  747. int reserve;
  748. unsigned long timeout_start = jiffies;
  749. if (!audit_initialized)
  750. return NULL;
  751. if (unlikely(audit_filter_type(type)))
  752. return NULL;
  753. if (gfp_mask & __GFP_WAIT)
  754. reserve = 0;
  755. else
  756. reserve = 5; /* Allow atomic callers to go up to five
  757. entries over the normal backlog limit */
  758. while (audit_backlog_limit
  759. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
  760. if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
  761. && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
  762. /* Wait for auditd to drain the queue a little */
  763. DECLARE_WAITQUEUE(wait, current);
  764. set_current_state(TASK_INTERRUPTIBLE);
  765. add_wait_queue(&audit_backlog_wait, &wait);
  766. if (audit_backlog_limit &&
  767. skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
  768. schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
  769. __set_current_state(TASK_RUNNING);
  770. remove_wait_queue(&audit_backlog_wait, &wait);
  771. continue;
  772. }
  773. if (audit_rate_check())
  774. printk(KERN_WARNING
  775. "audit: audit_backlog=%d > "
  776. "audit_backlog_limit=%d\n",
  777. skb_queue_len(&audit_skb_queue),
  778. audit_backlog_limit);
  779. audit_log_lost("backlog limit exceeded");
  780. audit_backlog_wait_time = audit_backlog_wait_overflow;
  781. wake_up(&audit_backlog_wait);
  782. return NULL;
  783. }
  784. ab = audit_buffer_alloc(ctx, gfp_mask, type);
  785. if (!ab) {
  786. audit_log_lost("out of memory in audit_log_start");
  787. return NULL;
  788. }
  789. audit_get_stamp(ab->ctx, &t, &serial);
  790. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  791. t.tv_sec, t.tv_nsec/1000000, serial);
  792. return ab;
  793. }
  794. /**
  795. * audit_expand - expand skb in the audit buffer
  796. * @ab: audit_buffer
  797. * @extra: space to add at tail of the skb
  798. *
  799. * Returns 0 (no space) on failed expansion, or available space if
  800. * successful.
  801. */
  802. static inline int audit_expand(struct audit_buffer *ab, int extra)
  803. {
  804. struct sk_buff *skb = ab->skb;
  805. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  806. ab->gfp_mask);
  807. if (ret < 0) {
  808. audit_log_lost("out of memory in audit_expand");
  809. return 0;
  810. }
  811. return skb_tailroom(skb);
  812. }
  813. /*
  814. * Format an audit message into the audit buffer. If there isn't enough
  815. * room in the audit buffer, more room will be allocated and vsnprint
  816. * will be called a second time. Currently, we assume that a printk
  817. * can't format message larger than 1024 bytes, so we don't either.
  818. */
  819. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  820. va_list args)
  821. {
  822. int len, avail;
  823. struct sk_buff *skb;
  824. va_list args2;
  825. if (!ab)
  826. return;
  827. BUG_ON(!ab->skb);
  828. skb = ab->skb;
  829. avail = skb_tailroom(skb);
  830. if (avail == 0) {
  831. avail = audit_expand(ab, AUDIT_BUFSIZ);
  832. if (!avail)
  833. goto out;
  834. }
  835. va_copy(args2, args);
  836. len = vsnprintf(skb->tail, avail, fmt, args);
  837. if (len >= avail) {
  838. /* The printk buffer is 1024 bytes long, so if we get
  839. * here and AUDIT_BUFSIZ is at least 1024, then we can
  840. * log everything that printk could have logged. */
  841. avail = audit_expand(ab,
  842. max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  843. if (!avail)
  844. goto out;
  845. len = vsnprintf(skb->tail, avail, fmt, args2);
  846. }
  847. if (len > 0)
  848. skb_put(skb, len);
  849. out:
  850. return;
  851. }
  852. /**
  853. * audit_log_format - format a message into the audit buffer.
  854. * @ab: audit_buffer
  855. * @fmt: format string
  856. * @...: optional parameters matching @fmt string
  857. *
  858. * All the work is done in audit_log_vformat.
  859. */
  860. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  861. {
  862. va_list args;
  863. if (!ab)
  864. return;
  865. va_start(args, fmt);
  866. audit_log_vformat(ab, fmt, args);
  867. va_end(args);
  868. }
  869. /**
  870. * audit_log_hex - convert a buffer to hex and append it to the audit skb
  871. * @ab: the audit_buffer
  872. * @buf: buffer to convert to hex
  873. * @len: length of @buf to be converted
  874. *
  875. * No return value; failure to expand is silently ignored.
  876. *
  877. * This function will take the passed buf and convert it into a string of
  878. * ascii hex digits. The new string is placed onto the skb.
  879. */
  880. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  881. size_t len)
  882. {
  883. int i, avail, new_len;
  884. unsigned char *ptr;
  885. struct sk_buff *skb;
  886. static const unsigned char *hex = "0123456789ABCDEF";
  887. BUG_ON(!ab->skb);
  888. skb = ab->skb;
  889. avail = skb_tailroom(skb);
  890. new_len = len<<1;
  891. if (new_len >= avail) {
  892. /* Round the buffer request up to the next multiple */
  893. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  894. avail = audit_expand(ab, new_len);
  895. if (!avail)
  896. return;
  897. }
  898. ptr = skb->tail;
  899. for (i=0; i<len; i++) {
  900. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  901. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  902. }
  903. *ptr = 0;
  904. skb_put(skb, len << 1); /* new string is twice the old string */
  905. }
  906. /**
  907. * audit_log_unstrustedstring - log a string that may contain random characters
  908. * @ab: audit_buffer
  909. * @string: string to be logged
  910. *
  911. * This code will escape a string that is passed to it if the string
  912. * contains a control character, unprintable character, double quote mark,
  913. * or a space. Unescaped strings will start and end with a double quote mark.
  914. * Strings that are escaped are printed in hex (2 digits per char).
  915. */
  916. const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  917. {
  918. const unsigned char *p = string;
  919. size_t len = strlen(string);
  920. while (*p) {
  921. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  922. audit_log_hex(ab, string, len);
  923. return string + len + 1;
  924. }
  925. p++;
  926. }
  927. audit_log_format(ab, "\"%s\"", string);
  928. return p + 1;
  929. }
  930. /* This is a helper-function to print the escaped d_path */
  931. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  932. struct dentry *dentry, struct vfsmount *vfsmnt)
  933. {
  934. char *p, *path;
  935. if (prefix)
  936. audit_log_format(ab, " %s", prefix);
  937. /* We will allow 11 spaces for ' (deleted)' to be appended */
  938. path = kmalloc(PATH_MAX+11, ab->gfp_mask);
  939. if (!path) {
  940. audit_log_format(ab, "<no memory>");
  941. return;
  942. }
  943. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  944. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  945. /* FIXME: can we save some information here? */
  946. audit_log_format(ab, "<too long>");
  947. } else
  948. audit_log_untrustedstring(ab, p);
  949. kfree(path);
  950. }
  951. /**
  952. * audit_log_end - end one audit record
  953. * @ab: the audit_buffer
  954. *
  955. * The netlink_* functions cannot be called inside an irq context, so
  956. * the audit buffer is placed on a queue and a tasklet is scheduled to
  957. * remove them from the queue outside the irq context. May be called in
  958. * any context.
  959. */
  960. void audit_log_end(struct audit_buffer *ab)
  961. {
  962. if (!ab)
  963. return;
  964. if (!audit_rate_check()) {
  965. audit_log_lost("rate limit exceeded");
  966. } else {
  967. if (audit_pid) {
  968. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  969. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  970. skb_queue_tail(&audit_skb_queue, ab->skb);
  971. ab->skb = NULL;
  972. wake_up_interruptible(&kauditd_wait);
  973. } else {
  974. printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
  975. }
  976. }
  977. audit_buffer_free(ab);
  978. }
  979. /**
  980. * audit_log - Log an audit record
  981. * @ctx: audit context
  982. * @gfp_mask: type of allocation
  983. * @type: audit message type
  984. * @fmt: format string to use
  985. * @...: variable parameters matching the format string
  986. *
  987. * This is a convenience function that calls audit_log_start,
  988. * audit_log_vformat, and audit_log_end. It may be called
  989. * in any context.
  990. */
  991. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  992. const char *fmt, ...)
  993. {
  994. struct audit_buffer *ab;
  995. va_list args;
  996. ab = audit_log_start(ctx, gfp_mask, type);
  997. if (ab) {
  998. va_start(args, fmt);
  999. audit_log_vformat(ab, fmt, args);
  1000. va_end(args);
  1001. audit_log_end(ab);
  1002. }
  1003. }
  1004. EXPORT_SYMBOL(audit_log_start);
  1005. EXPORT_SYMBOL(audit_log_end);
  1006. EXPORT_SYMBOL(audit_log_format);
  1007. EXPORT_SYMBOL(audit_log);