audit.c 40 KB

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