audit.c 54 KB

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