audit.c 41 KB

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