audit.c 53 KB

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