audit.c 53 KB

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