audit.c 45 KB

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