audit.c 53 KB

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