audit.c 36 KB

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