audit.c 30 KB

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