audit.c 41 KB

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