audit.c 39 KB

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