audit.c 58 KB

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