audit.c 32 KB

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