audit.c 51 KB

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