audit.c 36 KB

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