audit.c 48 KB

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