audit.c 51 KB

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