audit.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 <linux/freezer.h>
  58. #include "audit.h"
  59. /* No auditing will take place until audit_initialized != 0.
  60. * (Initialization happens after skb_init is called.) */
  61. static int audit_initialized;
  62. /* No syscall auditing will take place unless audit_enabled != 0. */
  63. int audit_enabled;
  64. /* Default state when kernel boots without any parameters. */
  65. static int audit_default;
  66. /* If auditing cannot proceed, audit_failure selects what happens. */
  67. static int audit_failure = AUDIT_FAIL_PRINTK;
  68. /* If audit records are to be written to the netlink socket, audit_pid
  69. * contains the (non-zero) pid. */
  70. int audit_pid;
  71. /* If audit_rate_limit is non-zero, limit the rate of sending audit records
  72. * to that number per second. This prevents DoS attacks, but results in
  73. * audit records being dropped. */
  74. static int audit_rate_limit;
  75. /* Number of outstanding audit_buffers allowed. */
  76. static int audit_backlog_limit = 64;
  77. static int audit_backlog_wait_time = 60 * HZ;
  78. static int audit_backlog_wait_overflow = 0;
  79. /* The identity of the user shutting down the audit system. */
  80. uid_t audit_sig_uid = -1;
  81. pid_t audit_sig_pid = -1;
  82. u32 audit_sig_sid = 0;
  83. /* Records can be lost in several ways:
  84. 0) [suppressed in audit_alloc]
  85. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  86. 2) out of memory in audit_log_move [alloc_skb]
  87. 3) suppressed due to audit_rate_limit
  88. 4) suppressed due to audit_backlog_limit
  89. */
  90. static atomic_t audit_lost = ATOMIC_INIT(0);
  91. /* The netlink socket. */
  92. static struct sock *audit_sock;
  93. /* Inotify handle. */
  94. struct inotify_handle *audit_ih;
  95. /* Hash for inode-based rules */
  96. struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
  97. /* The audit_freelist is a list of pre-allocated audit buffers (if more
  98. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  99. * being placed on the freelist). */
  100. static DEFINE_SPINLOCK(audit_freelist_lock);
  101. static int audit_freelist_count;
  102. static LIST_HEAD(audit_freelist);
  103. static struct sk_buff_head audit_skb_queue;
  104. static struct task_struct *kauditd_task;
  105. static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
  106. static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
  107. /* Serialize requests from userspace. */
  108. static DEFINE_MUTEX(audit_cmd_mutex);
  109. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  110. * audit records. Since printk uses a 1024 byte buffer, this buffer
  111. * should be at least that large. */
  112. #define AUDIT_BUFSIZ 1024
  113. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  114. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  115. #define AUDIT_MAXFREE (2*NR_CPUS)
  116. /* The audit_buffer is used when formatting an audit record. The caller
  117. * locks briefly to get the record off the freelist or to allocate the
  118. * buffer, and locks briefly to send the buffer to the netlink layer or
  119. * to place it on a transmit queue. Multiple audit_buffers can be in
  120. * use simultaneously. */
  121. struct audit_buffer {
  122. struct list_head list;
  123. struct sk_buff *skb; /* formatted skb ready to send */
  124. struct audit_context *ctx; /* NULL or associated context */
  125. gfp_t gfp_mask;
  126. };
  127. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  128. {
  129. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  130. nlh->nlmsg_pid = pid;
  131. }
  132. void audit_panic(const char *message)
  133. {
  134. switch (audit_failure)
  135. {
  136. case AUDIT_FAIL_SILENT:
  137. break;
  138. case AUDIT_FAIL_PRINTK:
  139. printk(KERN_ERR "audit: %s\n", message);
  140. break;
  141. case AUDIT_FAIL_PANIC:
  142. panic("audit: %s\n", message);
  143. break;
  144. }
  145. }
  146. static inline int audit_rate_check(void)
  147. {
  148. static unsigned long last_check = 0;
  149. static int messages = 0;
  150. static DEFINE_SPINLOCK(lock);
  151. unsigned long flags;
  152. unsigned long now;
  153. unsigned long elapsed;
  154. int retval = 0;
  155. if (!audit_rate_limit) return 1;
  156. spin_lock_irqsave(&lock, flags);
  157. if (++messages < audit_rate_limit) {
  158. retval = 1;
  159. } else {
  160. now = jiffies;
  161. elapsed = now - last_check;
  162. if (elapsed > HZ) {
  163. last_check = now;
  164. messages = 0;
  165. retval = 1;
  166. }
  167. }
  168. spin_unlock_irqrestore(&lock, flags);
  169. return retval;
  170. }
  171. /**
  172. * audit_log_lost - conditionally log lost audit message event
  173. * @message: the message stating reason for lost audit message
  174. *
  175. * Emit at least 1 message per second, even if audit_rate_check is
  176. * throttling.
  177. * Always increment the lost messages counter.
  178. */
  179. void audit_log_lost(const char *message)
  180. {
  181. static unsigned long last_msg = 0;
  182. static DEFINE_SPINLOCK(lock);
  183. unsigned long flags;
  184. unsigned long now;
  185. int print;
  186. atomic_inc(&audit_lost);
  187. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  188. if (!print) {
  189. spin_lock_irqsave(&lock, flags);
  190. now = jiffies;
  191. if (now - last_msg > HZ) {
  192. print = 1;
  193. last_msg = now;
  194. }
  195. spin_unlock_irqrestore(&lock, flags);
  196. }
  197. if (print) {
  198. printk(KERN_WARNING
  199. "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
  200. atomic_read(&audit_lost),
  201. audit_rate_limit,
  202. audit_backlog_limit);
  203. audit_panic(message);
  204. }
  205. }
  206. static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
  207. {
  208. int old = audit_rate_limit;
  209. if (sid) {
  210. char *ctx = NULL;
  211. u32 len;
  212. int rc;
  213. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  214. return rc;
  215. else
  216. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  217. "audit_rate_limit=%d old=%d by auid=%u subj=%s",
  218. limit, old, loginuid, ctx);
  219. kfree(ctx);
  220. } else
  221. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  222. "audit_rate_limit=%d old=%d by auid=%u",
  223. limit, old, loginuid);
  224. audit_rate_limit = limit;
  225. return 0;
  226. }
  227. static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
  228. {
  229. int old = audit_backlog_limit;
  230. if (sid) {
  231. char *ctx = NULL;
  232. u32 len;
  233. int rc;
  234. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  235. return rc;
  236. else
  237. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  238. "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
  239. limit, old, loginuid, ctx);
  240. kfree(ctx);
  241. } else
  242. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  243. "audit_backlog_limit=%d old=%d by auid=%u",
  244. limit, old, loginuid);
  245. audit_backlog_limit = limit;
  246. return 0;
  247. }
  248. static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
  249. {
  250. int old = audit_enabled;
  251. if (state != 0 && state != 1)
  252. return -EINVAL;
  253. if (sid) {
  254. char *ctx = NULL;
  255. u32 len;
  256. int rc;
  257. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  258. return rc;
  259. else
  260. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  261. "audit_enabled=%d old=%d by auid=%u subj=%s",
  262. state, old, loginuid, ctx);
  263. kfree(ctx);
  264. } else
  265. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  266. "audit_enabled=%d old=%d by auid=%u",
  267. state, old, loginuid);
  268. audit_enabled = state;
  269. return 0;
  270. }
  271. static int audit_set_failure(int state, uid_t loginuid, u32 sid)
  272. {
  273. int old = audit_failure;
  274. if (state != AUDIT_FAIL_SILENT
  275. && state != AUDIT_FAIL_PRINTK
  276. && state != AUDIT_FAIL_PANIC)
  277. return -EINVAL;
  278. if (sid) {
  279. char *ctx = NULL;
  280. u32 len;
  281. int rc;
  282. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  283. return rc;
  284. else
  285. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  286. "audit_failure=%d old=%d by auid=%u subj=%s",
  287. state, old, loginuid, ctx);
  288. kfree(ctx);
  289. } else
  290. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  291. "audit_failure=%d old=%d by auid=%u",
  292. state, old, loginuid);
  293. audit_failure = state;
  294. return 0;
  295. }
  296. static int kauditd_thread(void *dummy)
  297. {
  298. struct sk_buff *skb;
  299. while (!kthread_should_stop()) {
  300. skb = skb_dequeue(&audit_skb_queue);
  301. wake_up(&audit_backlog_wait);
  302. if (skb) {
  303. if (audit_pid) {
  304. int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
  305. if (err < 0) {
  306. BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
  307. printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
  308. audit_pid = 0;
  309. }
  310. } else {
  311. printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
  312. kfree_skb(skb);
  313. }
  314. } else {
  315. DECLARE_WAITQUEUE(wait, current);
  316. set_current_state(TASK_INTERRUPTIBLE);
  317. add_wait_queue(&kauditd_wait, &wait);
  318. if (!skb_queue_len(&audit_skb_queue)) {
  319. try_to_freeze();
  320. schedule();
  321. }
  322. __set_current_state(TASK_RUNNING);
  323. remove_wait_queue(&kauditd_wait, &wait);
  324. }
  325. }
  326. return 0;
  327. }
  328. int audit_send_list(void *_dest)
  329. {
  330. struct audit_netlink_list *dest = _dest;
  331. int pid = dest->pid;
  332. struct sk_buff *skb;
  333. /* wait for parent to finish and send an ACK */
  334. mutex_lock(&audit_cmd_mutex);
  335. mutex_unlock(&audit_cmd_mutex);
  336. while ((skb = __skb_dequeue(&dest->q)) != NULL)
  337. netlink_unicast(audit_sock, skb, pid, 0);
  338. kfree(dest);
  339. return 0;
  340. }
  341. struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
  342. int multi, void *payload, int size)
  343. {
  344. struct sk_buff *skb;
  345. struct nlmsghdr *nlh;
  346. int len = NLMSG_SPACE(size);
  347. void *data;
  348. int flags = multi ? NLM_F_MULTI : 0;
  349. int t = done ? NLMSG_DONE : type;
  350. skb = alloc_skb(len, GFP_KERNEL);
  351. if (!skb)
  352. return NULL;
  353. nlh = NLMSG_PUT(skb, pid, seq, t, size);
  354. nlh->nlmsg_flags = flags;
  355. data = NLMSG_DATA(nlh);
  356. memcpy(data, payload, size);
  357. return skb;
  358. nlmsg_failure: /* Used by NLMSG_PUT */
  359. if (skb)
  360. kfree_skb(skb);
  361. return NULL;
  362. }
  363. /**
  364. * audit_send_reply - send an audit reply message via netlink
  365. * @pid: process id to send reply to
  366. * @seq: sequence number
  367. * @type: audit message type
  368. * @done: done (last) flag
  369. * @multi: multi-part message flag
  370. * @payload: payload data
  371. * @size: payload size
  372. *
  373. * Allocates an skb, builds the netlink message, and sends it to the pid.
  374. * No failure notifications.
  375. */
  376. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  377. void *payload, int size)
  378. {
  379. struct sk_buff *skb;
  380. skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
  381. if (!skb)
  382. return;
  383. /* Ignore failure. It'll only happen if the sender goes away,
  384. because our timeout is set to infinite. */
  385. netlink_unicast(audit_sock, skb, pid, 0);
  386. return;
  387. }
  388. /*
  389. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  390. * control messages.
  391. */
  392. static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
  393. {
  394. int err = 0;
  395. switch (msg_type) {
  396. case AUDIT_GET:
  397. case AUDIT_LIST:
  398. case AUDIT_LIST_RULES:
  399. case AUDIT_SET:
  400. case AUDIT_ADD:
  401. case AUDIT_ADD_RULE:
  402. case AUDIT_DEL:
  403. case AUDIT_DEL_RULE:
  404. case AUDIT_SIGNAL_INFO:
  405. if (security_netlink_recv(skb, CAP_AUDIT_CONTROL))
  406. err = -EPERM;
  407. break;
  408. case AUDIT_USER:
  409. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  410. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  411. if (security_netlink_recv(skb, CAP_AUDIT_WRITE))
  412. err = -EPERM;
  413. break;
  414. default: /* bad msg */
  415. err = -EINVAL;
  416. }
  417. return err;
  418. }
  419. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  420. {
  421. u32 uid, pid, seq, sid;
  422. void *data;
  423. struct audit_status *status_get, status_set;
  424. int err;
  425. struct audit_buffer *ab;
  426. u16 msg_type = nlh->nlmsg_type;
  427. uid_t loginuid; /* loginuid of sender */
  428. struct audit_sig_info *sig_data;
  429. char *ctx;
  430. u32 len;
  431. err = audit_netlink_ok(skb, msg_type);
  432. if (err)
  433. return err;
  434. /* As soon as there's any sign of userspace auditd,
  435. * start kauditd to talk to it */
  436. if (!kauditd_task)
  437. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  438. if (IS_ERR(kauditd_task)) {
  439. err = PTR_ERR(kauditd_task);
  440. kauditd_task = NULL;
  441. return err;
  442. }
  443. pid = NETLINK_CREDS(skb)->pid;
  444. uid = NETLINK_CREDS(skb)->uid;
  445. loginuid = NETLINK_CB(skb).loginuid;
  446. sid = NETLINK_CB(skb).sid;
  447. seq = nlh->nlmsg_seq;
  448. data = NLMSG_DATA(nlh);
  449. switch (msg_type) {
  450. case AUDIT_GET:
  451. status_set.enabled = audit_enabled;
  452. status_set.failure = audit_failure;
  453. status_set.pid = audit_pid;
  454. status_set.rate_limit = audit_rate_limit;
  455. status_set.backlog_limit = audit_backlog_limit;
  456. status_set.lost = atomic_read(&audit_lost);
  457. status_set.backlog = skb_queue_len(&audit_skb_queue);
  458. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  459. &status_set, sizeof(status_set));
  460. break;
  461. case AUDIT_SET:
  462. if (nlh->nlmsg_len < sizeof(struct audit_status))
  463. return -EINVAL;
  464. status_get = (struct audit_status *)data;
  465. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  466. err = audit_set_enabled(status_get->enabled,
  467. loginuid, sid);
  468. if (err < 0) return err;
  469. }
  470. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  471. err = audit_set_failure(status_get->failure,
  472. loginuid, sid);
  473. if (err < 0) return err;
  474. }
  475. if (status_get->mask & AUDIT_STATUS_PID) {
  476. int old = audit_pid;
  477. if (sid) {
  478. if ((err = selinux_sid_to_string(
  479. sid, &ctx, &len)))
  480. return err;
  481. else
  482. audit_log(NULL, GFP_KERNEL,
  483. AUDIT_CONFIG_CHANGE,
  484. "audit_pid=%d old=%d by auid=%u subj=%s",
  485. status_get->pid, old,
  486. loginuid, ctx);
  487. kfree(ctx);
  488. } else
  489. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  490. "audit_pid=%d old=%d by auid=%u",
  491. status_get->pid, old, loginuid);
  492. audit_pid = status_get->pid;
  493. }
  494. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  495. err = audit_set_rate_limit(status_get->rate_limit,
  496. loginuid, sid);
  497. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  498. err = audit_set_backlog_limit(status_get->backlog_limit,
  499. loginuid, sid);
  500. break;
  501. case AUDIT_USER:
  502. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  503. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  504. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  505. return 0;
  506. err = audit_filter_user(&NETLINK_CB(skb), msg_type);
  507. if (err == 1) {
  508. err = 0;
  509. ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  510. if (ab) {
  511. audit_log_format(ab,
  512. "user pid=%d uid=%u auid=%u",
  513. pid, uid, loginuid);
  514. if (sid) {
  515. if (selinux_sid_to_string(
  516. sid, &ctx, &len)) {
  517. audit_log_format(ab,
  518. " ssid=%u", sid);
  519. /* Maybe call audit_panic? */
  520. } else
  521. audit_log_format(ab,
  522. " subj=%s", ctx);
  523. kfree(ctx);
  524. }
  525. audit_log_format(ab, " msg='%.1024s'",
  526. (char *)data);
  527. audit_set_pid(ab, pid);
  528. audit_log_end(ab);
  529. }
  530. }
  531. break;
  532. case AUDIT_ADD:
  533. case AUDIT_DEL:
  534. if (nlmsg_len(nlh) < sizeof(struct audit_rule))
  535. return -EINVAL;
  536. /* fallthrough */
  537. case AUDIT_LIST:
  538. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  539. uid, seq, data, nlmsg_len(nlh),
  540. loginuid, sid);
  541. break;
  542. case AUDIT_ADD_RULE:
  543. case AUDIT_DEL_RULE:
  544. if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
  545. return -EINVAL;
  546. /* fallthrough */
  547. case AUDIT_LIST_RULES:
  548. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  549. uid, seq, data, nlmsg_len(nlh),
  550. loginuid, sid);
  551. break;
  552. case AUDIT_SIGNAL_INFO:
  553. err = selinux_sid_to_string(audit_sig_sid, &ctx, &len);
  554. if (err)
  555. return err;
  556. sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
  557. if (!sig_data) {
  558. kfree(ctx);
  559. return -ENOMEM;
  560. }
  561. sig_data->uid = audit_sig_uid;
  562. sig_data->pid = audit_sig_pid;
  563. memcpy(sig_data->ctx, ctx, len);
  564. kfree(ctx);
  565. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  566. 0, 0, sig_data, sizeof(*sig_data) + len);
  567. kfree(sig_data);
  568. break;
  569. default:
  570. err = -EINVAL;
  571. break;
  572. }
  573. return err < 0 ? err : 0;
  574. }
  575. /*
  576. * Get message from skb (based on rtnetlink_rcv_skb). Each message is
  577. * processed by audit_receive_msg. Malformed skbs with wrong length are
  578. * discarded silently.
  579. */
  580. static void audit_receive_skb(struct sk_buff *skb)
  581. {
  582. int err;
  583. struct nlmsghdr *nlh;
  584. u32 rlen;
  585. while (skb->len >= NLMSG_SPACE(0)) {
  586. nlh = (struct nlmsghdr *)skb->data;
  587. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  588. return;
  589. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  590. if (rlen > skb->len)
  591. rlen = skb->len;
  592. if ((err = audit_receive_msg(skb, nlh))) {
  593. netlink_ack(skb, nlh, err);
  594. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  595. netlink_ack(skb, nlh, 0);
  596. skb_pull(skb, rlen);
  597. }
  598. }
  599. /* Receive messages from netlink socket. */
  600. static void audit_receive(struct sock *sk, int length)
  601. {
  602. struct sk_buff *skb;
  603. unsigned int qlen;
  604. mutex_lock(&audit_cmd_mutex);
  605. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  606. skb = skb_dequeue(&sk->sk_receive_queue);
  607. audit_receive_skb(skb);
  608. kfree_skb(skb);
  609. }
  610. mutex_unlock(&audit_cmd_mutex);
  611. }
  612. #ifdef CONFIG_AUDITSYSCALL
  613. static const struct inotify_operations audit_inotify_ops = {
  614. .handle_event = audit_handle_ievent,
  615. .destroy_watch = audit_free_parent,
  616. };
  617. #endif
  618. /* Initialize audit support at boot time. */
  619. static int __init audit_init(void)
  620. {
  621. int i;
  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. #endif
  642. for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
  643. INIT_LIST_HEAD(&audit_inode_hash[i]);
  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 DEFINE_SPINLOCK(serial_lock);
  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. if (!ab)
  917. return;
  918. BUG_ON(!ab->skb);
  919. skb = ab->skb;
  920. avail = skb_tailroom(skb);
  921. new_len = len<<1;
  922. if (new_len >= avail) {
  923. /* Round the buffer request up to the next multiple */
  924. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  925. avail = audit_expand(ab, new_len);
  926. if (!avail)
  927. return;
  928. }
  929. ptr = skb->tail;
  930. for (i=0; i<len; i++) {
  931. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  932. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  933. }
  934. *ptr = 0;
  935. skb_put(skb, len << 1); /* new string is twice the old string */
  936. }
  937. /*
  938. * Format a string of no more than slen characters into the audit buffer,
  939. * enclosed in quote marks.
  940. */
  941. static void audit_log_n_string(struct audit_buffer *ab, size_t slen,
  942. const char *string)
  943. {
  944. int avail, new_len;
  945. unsigned char *ptr;
  946. struct sk_buff *skb;
  947. if (!ab)
  948. return;
  949. BUG_ON(!ab->skb);
  950. skb = ab->skb;
  951. avail = skb_tailroom(skb);
  952. new_len = slen + 3; /* enclosing quotes + null terminator */
  953. if (new_len > avail) {
  954. avail = audit_expand(ab, new_len);
  955. if (!avail)
  956. return;
  957. }
  958. ptr = skb->tail;
  959. *ptr++ = '"';
  960. memcpy(ptr, string, slen);
  961. ptr += slen;
  962. *ptr++ = '"';
  963. *ptr = 0;
  964. skb_put(skb, slen + 2); /* don't include null terminator */
  965. }
  966. /**
  967. * audit_log_n_unstrustedstring - log a string that may contain random characters
  968. * @ab: audit_buffer
  969. * @len: lenth of string (not including trailing null)
  970. * @string: string to be logged
  971. *
  972. * This code will escape a string that is passed to it if the string
  973. * contains a control character, unprintable character, double quote mark,
  974. * or a space. Unescaped strings will start and end with a double quote mark.
  975. * Strings that are escaped are printed in hex (2 digits per char).
  976. *
  977. * The caller specifies the number of characters in the string to log, which may
  978. * or may not be the entire string.
  979. */
  980. const char *audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len,
  981. const char *string)
  982. {
  983. const unsigned char *p = string;
  984. while (*p) {
  985. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  986. audit_log_hex(ab, string, len);
  987. return string + len + 1;
  988. }
  989. p++;
  990. }
  991. audit_log_n_string(ab, len, string);
  992. return p + 1;
  993. }
  994. /**
  995. * audit_log_unstrustedstring - log a string that may contain random characters
  996. * @ab: audit_buffer
  997. * @string: string to be logged
  998. *
  999. * Same as audit_log_n_unstrustedstring(), except that strlen is used to
  1000. * determine string length.
  1001. */
  1002. const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  1003. {
  1004. return audit_log_n_untrustedstring(ab, strlen(string), string);
  1005. }
  1006. /* This is a helper-function to print the escaped d_path */
  1007. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  1008. struct dentry *dentry, struct vfsmount *vfsmnt)
  1009. {
  1010. char *p, *path;
  1011. if (prefix)
  1012. audit_log_format(ab, " %s", prefix);
  1013. /* We will allow 11 spaces for ' (deleted)' to be appended */
  1014. path = kmalloc(PATH_MAX+11, ab->gfp_mask);
  1015. if (!path) {
  1016. audit_log_format(ab, "<no memory>");
  1017. return;
  1018. }
  1019. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  1020. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  1021. /* FIXME: can we save some information here? */
  1022. audit_log_format(ab, "<too long>");
  1023. } else
  1024. audit_log_untrustedstring(ab, p);
  1025. kfree(path);
  1026. }
  1027. /**
  1028. * audit_log_end - end one audit record
  1029. * @ab: the audit_buffer
  1030. *
  1031. * The netlink_* functions cannot be called inside an irq context, so
  1032. * the audit buffer is placed on a queue and a tasklet is scheduled to
  1033. * remove them from the queue outside the irq context. May be called in
  1034. * any context.
  1035. */
  1036. void audit_log_end(struct audit_buffer *ab)
  1037. {
  1038. if (!ab)
  1039. return;
  1040. if (!audit_rate_check()) {
  1041. audit_log_lost("rate limit exceeded");
  1042. } else {
  1043. if (audit_pid) {
  1044. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  1045. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  1046. skb_queue_tail(&audit_skb_queue, ab->skb);
  1047. ab->skb = NULL;
  1048. wake_up_interruptible(&kauditd_wait);
  1049. } else {
  1050. printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
  1051. }
  1052. }
  1053. audit_buffer_free(ab);
  1054. }
  1055. /**
  1056. * audit_log - Log an audit record
  1057. * @ctx: audit context
  1058. * @gfp_mask: type of allocation
  1059. * @type: audit message type
  1060. * @fmt: format string to use
  1061. * @...: variable parameters matching the format string
  1062. *
  1063. * This is a convenience function that calls audit_log_start,
  1064. * audit_log_vformat, and audit_log_end. It may be called
  1065. * in any context.
  1066. */
  1067. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  1068. const char *fmt, ...)
  1069. {
  1070. struct audit_buffer *ab;
  1071. va_list args;
  1072. ab = audit_log_start(ctx, gfp_mask, type);
  1073. if (ab) {
  1074. va_start(args, fmt);
  1075. audit_log_vformat(ab, fmt, args);
  1076. va_end(args);
  1077. audit_log_end(ab);
  1078. }
  1079. }
  1080. EXPORT_SYMBOL(audit_log_start);
  1081. EXPORT_SYMBOL(audit_log_end);
  1082. EXPORT_SYMBOL(audit_log_format);
  1083. EXPORT_SYMBOL(audit_log);