audit.c 41 KB

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