audit.c 37 KB

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