audit.c 37 KB

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