audit.c 47 KB

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