audit.c 53 KB

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