audit.c 36 KB

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