audit.c 37 KB

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