audit.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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-2004 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 SELinux.
  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 <asm/atomic.h>
  46. #include <linux/mm.h>
  47. #include <linux/module.h>
  48. #include <linux/err.h>
  49. #include <linux/kthread.h>
  50. #include <linux/audit.h>
  51. #include <net/sock.h>
  52. #include <net/netlink.h>
  53. #include <linux/skbuff.h>
  54. #include <linux/netlink.h>
  55. #include <linux/selinux.h>
  56. #include <linux/inotify.h>
  57. #include "audit.h"
  58. /* No auditing will take place until audit_initialized != 0.
  59. * (Initialization happens after skb_init is called.) */
  60. static int audit_initialized;
  61. /* No syscall auditing will take place unless audit_enabled != 0. */
  62. int audit_enabled;
  63. /* Default state when kernel boots without any parameters. */
  64. static int audit_default;
  65. /* If auditing cannot proceed, audit_failure selects what happens. */
  66. static int audit_failure = AUDIT_FAIL_PRINTK;
  67. /* If audit records are to be written to the netlink socket, audit_pid
  68. * contains the (non-zero) pid. */
  69. int audit_pid;
  70. /* If audit_rate_limit is non-zero, limit the rate of sending audit records
  71. * to that number per second. This prevents DoS attacks, but results in
  72. * audit records being dropped. */
  73. static int audit_rate_limit;
  74. /* Number of outstanding audit_buffers allowed. */
  75. static int audit_backlog_limit = 64;
  76. static int audit_backlog_wait_time = 60 * HZ;
  77. static int audit_backlog_wait_overflow = 0;
  78. /* The identity of the user shutting down the audit system. */
  79. uid_t audit_sig_uid = -1;
  80. pid_t audit_sig_pid = -1;
  81. u32 audit_sig_sid = 0;
  82. /* Records can be lost in several ways:
  83. 0) [suppressed in audit_alloc]
  84. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  85. 2) out of memory in audit_log_move [alloc_skb]
  86. 3) suppressed due to audit_rate_limit
  87. 4) suppressed due to audit_backlog_limit
  88. */
  89. static atomic_t audit_lost = ATOMIC_INIT(0);
  90. /* The netlink socket. */
  91. static struct sock *audit_sock;
  92. /* Inotify handle. */
  93. struct inotify_handle *audit_ih;
  94. /* Hash for inode-based rules */
  95. struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
  96. /* The audit_freelist is a list of pre-allocated audit buffers (if more
  97. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  98. * being placed on the freelist). */
  99. static DEFINE_SPINLOCK(audit_freelist_lock);
  100. static int audit_freelist_count;
  101. static LIST_HEAD(audit_freelist);
  102. static struct sk_buff_head audit_skb_queue;
  103. static struct task_struct *kauditd_task;
  104. static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
  105. static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
  106. /* Serialize requests from userspace. */
  107. static DEFINE_MUTEX(audit_cmd_mutex);
  108. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  109. * audit records. Since printk uses a 1024 byte buffer, this buffer
  110. * should be at least that large. */
  111. #define AUDIT_BUFSIZ 1024
  112. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  113. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  114. #define AUDIT_MAXFREE (2*NR_CPUS)
  115. /* The audit_buffer is used when formatting an audit record. The caller
  116. * locks briefly to get the record off the freelist or to allocate the
  117. * buffer, and locks briefly to send the buffer to the netlink layer or
  118. * to place it on a transmit queue. Multiple audit_buffers can be in
  119. * use simultaneously. */
  120. struct audit_buffer {
  121. struct list_head list;
  122. struct sk_buff *skb; /* formatted skb ready to send */
  123. struct audit_context *ctx; /* NULL or associated context */
  124. gfp_t gfp_mask;
  125. };
  126. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  127. {
  128. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  129. nlh->nlmsg_pid = pid;
  130. }
  131. void audit_panic(const char *message)
  132. {
  133. switch (audit_failure)
  134. {
  135. case AUDIT_FAIL_SILENT:
  136. break;
  137. case AUDIT_FAIL_PRINTK:
  138. printk(KERN_ERR "audit: %s\n", message);
  139. break;
  140. case AUDIT_FAIL_PANIC:
  141. panic("audit: %s\n", message);
  142. break;
  143. }
  144. }
  145. static inline int audit_rate_check(void)
  146. {
  147. static unsigned long last_check = 0;
  148. static int messages = 0;
  149. static DEFINE_SPINLOCK(lock);
  150. unsigned long flags;
  151. unsigned long now;
  152. unsigned long elapsed;
  153. int retval = 0;
  154. if (!audit_rate_limit) return 1;
  155. spin_lock_irqsave(&lock, flags);
  156. if (++messages < audit_rate_limit) {
  157. retval = 1;
  158. } else {
  159. now = jiffies;
  160. elapsed = now - last_check;
  161. if (elapsed > HZ) {
  162. last_check = now;
  163. messages = 0;
  164. retval = 1;
  165. }
  166. }
  167. spin_unlock_irqrestore(&lock, flags);
  168. return retval;
  169. }
  170. /**
  171. * audit_log_lost - conditionally log lost audit message event
  172. * @message: the message stating reason for lost audit message
  173. *
  174. * Emit at least 1 message per second, even if audit_rate_check is
  175. * throttling.
  176. * Always increment the lost messages counter.
  177. */
  178. void audit_log_lost(const char *message)
  179. {
  180. static unsigned long last_msg = 0;
  181. static DEFINE_SPINLOCK(lock);
  182. unsigned long flags;
  183. unsigned long now;
  184. int print;
  185. atomic_inc(&audit_lost);
  186. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  187. if (!print) {
  188. spin_lock_irqsave(&lock, flags);
  189. now = jiffies;
  190. if (now - last_msg > HZ) {
  191. print = 1;
  192. last_msg = now;
  193. }
  194. spin_unlock_irqrestore(&lock, flags);
  195. }
  196. if (print) {
  197. printk(KERN_WARNING
  198. "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
  199. atomic_read(&audit_lost),
  200. audit_rate_limit,
  201. audit_backlog_limit);
  202. audit_panic(message);
  203. }
  204. }
  205. static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
  206. {
  207. int old = audit_rate_limit;
  208. if (sid) {
  209. char *ctx = NULL;
  210. u32 len;
  211. int rc;
  212. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  213. return rc;
  214. else
  215. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  216. "audit_rate_limit=%d old=%d by auid=%u subj=%s",
  217. limit, old, loginuid, ctx);
  218. kfree(ctx);
  219. } else
  220. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  221. "audit_rate_limit=%d old=%d by auid=%u",
  222. limit, old, loginuid);
  223. audit_rate_limit = limit;
  224. return 0;
  225. }
  226. static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
  227. {
  228. int old = audit_backlog_limit;
  229. if (sid) {
  230. char *ctx = NULL;
  231. u32 len;
  232. int rc;
  233. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  234. return rc;
  235. else
  236. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  237. "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
  238. limit, old, loginuid, ctx);
  239. kfree(ctx);
  240. } else
  241. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  242. "audit_backlog_limit=%d old=%d by auid=%u",
  243. limit, old, loginuid);
  244. audit_backlog_limit = limit;
  245. return 0;
  246. }
  247. static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
  248. {
  249. int old = audit_enabled;
  250. if (state != 0 && state != 1)
  251. return -EINVAL;
  252. if (sid) {
  253. char *ctx = NULL;
  254. u32 len;
  255. int rc;
  256. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  257. return rc;
  258. else
  259. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  260. "audit_enabled=%d old=%d by auid=%u subj=%s",
  261. state, old, loginuid, ctx);
  262. kfree(ctx);
  263. } else
  264. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  265. "audit_enabled=%d old=%d by auid=%u",
  266. state, old, loginuid);
  267. audit_enabled = state;
  268. return 0;
  269. }
  270. static int audit_set_failure(int state, uid_t loginuid, u32 sid)
  271. {
  272. int old = audit_failure;
  273. if (state != AUDIT_FAIL_SILENT
  274. && state != AUDIT_FAIL_PRINTK
  275. && state != AUDIT_FAIL_PANIC)
  276. return -EINVAL;
  277. if (sid) {
  278. char *ctx = NULL;
  279. u32 len;
  280. int rc;
  281. if ((rc = selinux_sid_to_string(sid, &ctx, &len)))
  282. return rc;
  283. else
  284. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  285. "audit_failure=%d old=%d by auid=%u subj=%s",
  286. state, old, loginuid, ctx);
  287. kfree(ctx);
  288. } else
  289. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  290. "audit_failure=%d old=%d by auid=%u",
  291. state, old, loginuid);
  292. audit_failure = state;
  293. return 0;
  294. }
  295. static int kauditd_thread(void *dummy)
  296. {
  297. struct sk_buff *skb;
  298. while (!kthread_should_stop()) {
  299. skb = skb_dequeue(&audit_skb_queue);
  300. wake_up(&audit_backlog_wait);
  301. if (skb) {
  302. if (audit_pid) {
  303. int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
  304. if (err < 0) {
  305. BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
  306. printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
  307. audit_pid = 0;
  308. }
  309. } else {
  310. printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
  311. kfree_skb(skb);
  312. }
  313. } else {
  314. DECLARE_WAITQUEUE(wait, current);
  315. set_current_state(TASK_INTERRUPTIBLE);
  316. add_wait_queue(&kauditd_wait, &wait);
  317. if (!skb_queue_len(&audit_skb_queue)) {
  318. try_to_freeze();
  319. schedule();
  320. }
  321. __set_current_state(TASK_RUNNING);
  322. remove_wait_queue(&kauditd_wait, &wait);
  323. }
  324. }
  325. return 0;
  326. }
  327. int audit_send_list(void *_dest)
  328. {
  329. struct audit_netlink_list *dest = _dest;
  330. int pid = dest->pid;
  331. struct sk_buff *skb;
  332. /* wait for parent to finish and send an ACK */
  333. mutex_lock(&audit_cmd_mutex);
  334. mutex_unlock(&audit_cmd_mutex);
  335. while ((skb = __skb_dequeue(&dest->q)) != NULL)
  336. netlink_unicast(audit_sock, skb, pid, 0);
  337. kfree(dest);
  338. return 0;
  339. }
  340. struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
  341. int multi, void *payload, int size)
  342. {
  343. struct sk_buff *skb;
  344. struct nlmsghdr *nlh;
  345. int len = NLMSG_SPACE(size);
  346. void *data;
  347. int flags = multi ? NLM_F_MULTI : 0;
  348. int t = done ? NLMSG_DONE : type;
  349. skb = alloc_skb(len, GFP_KERNEL);
  350. if (!skb)
  351. return NULL;
  352. nlh = NLMSG_PUT(skb, pid, seq, t, size);
  353. nlh->nlmsg_flags = flags;
  354. data = NLMSG_DATA(nlh);
  355. memcpy(data, payload, size);
  356. return skb;
  357. nlmsg_failure: /* Used by NLMSG_PUT */
  358. if (skb)
  359. kfree_skb(skb);
  360. return NULL;
  361. }
  362. /**
  363. * audit_send_reply - send an audit reply message via netlink
  364. * @pid: process id to send reply to
  365. * @seq: sequence number
  366. * @type: audit message type
  367. * @done: done (last) flag
  368. * @multi: multi-part message flag
  369. * @payload: payload data
  370. * @size: payload size
  371. *
  372. * Allocates an skb, builds the netlink message, and sends it to the pid.
  373. * No failure notifications.
  374. */
  375. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  376. void *payload, int size)
  377. {
  378. struct sk_buff *skb;
  379. skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
  380. if (!skb)
  381. return;
  382. /* Ignore failure. It'll only happen if the sender goes away,
  383. because our timeout is set to infinite. */
  384. netlink_unicast(audit_sock, skb, pid, 0);
  385. return;
  386. }
  387. /*
  388. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  389. * control messages.
  390. */
  391. static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
  392. {
  393. int err = 0;
  394. switch (msg_type) {
  395. case AUDIT_GET:
  396. case AUDIT_LIST:
  397. case AUDIT_LIST_RULES:
  398. case AUDIT_SET:
  399. case AUDIT_ADD:
  400. case AUDIT_ADD_RULE:
  401. case AUDIT_DEL:
  402. case AUDIT_DEL_RULE:
  403. case AUDIT_SIGNAL_INFO:
  404. if (security_netlink_recv(skb, CAP_AUDIT_CONTROL))
  405. err = -EPERM;
  406. break;
  407. case AUDIT_USER:
  408. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  409. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  410. if (security_netlink_recv(skb, CAP_AUDIT_WRITE))
  411. err = -EPERM;
  412. break;
  413. default: /* bad msg */
  414. err = -EINVAL;
  415. }
  416. return err;
  417. }
  418. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  419. {
  420. u32 uid, pid, seq, sid;
  421. void *data;
  422. struct audit_status *status_get, status_set;
  423. int err;
  424. struct audit_buffer *ab;
  425. u16 msg_type = nlh->nlmsg_type;
  426. uid_t loginuid; /* loginuid of sender */
  427. struct audit_sig_info *sig_data;
  428. char *ctx;
  429. u32 len;
  430. err = audit_netlink_ok(skb, msg_type);
  431. if (err)
  432. return err;
  433. /* As soon as there's any sign of userspace auditd,
  434. * start kauditd to talk to it */
  435. if (!kauditd_task)
  436. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  437. if (IS_ERR(kauditd_task)) {
  438. err = PTR_ERR(kauditd_task);
  439. kauditd_task = NULL;
  440. return err;
  441. }
  442. pid = NETLINK_CREDS(skb)->pid;
  443. uid = NETLINK_CREDS(skb)->uid;
  444. loginuid = NETLINK_CB(skb).loginuid;
  445. sid = NETLINK_CB(skb).sid;
  446. seq = nlh->nlmsg_seq;
  447. data = NLMSG_DATA(nlh);
  448. switch (msg_type) {
  449. case AUDIT_GET:
  450. status_set.enabled = audit_enabled;
  451. status_set.failure = audit_failure;
  452. status_set.pid = audit_pid;
  453. status_set.rate_limit = audit_rate_limit;
  454. status_set.backlog_limit = audit_backlog_limit;
  455. status_set.lost = atomic_read(&audit_lost);
  456. status_set.backlog = skb_queue_len(&audit_skb_queue);
  457. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  458. &status_set, sizeof(status_set));
  459. break;
  460. case AUDIT_SET:
  461. if (nlh->nlmsg_len < sizeof(struct audit_status))
  462. return -EINVAL;
  463. status_get = (struct audit_status *)data;
  464. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  465. err = audit_set_enabled(status_get->enabled,
  466. loginuid, sid);
  467. if (err < 0) return err;
  468. }
  469. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  470. err = audit_set_failure(status_get->failure,
  471. loginuid, sid);
  472. if (err < 0) return err;
  473. }
  474. if (status_get->mask & AUDIT_STATUS_PID) {
  475. int old = audit_pid;
  476. if (sid) {
  477. if ((err = selinux_sid_to_string(
  478. sid, &ctx, &len)))
  479. return err;
  480. else
  481. audit_log(NULL, GFP_KERNEL,
  482. AUDIT_CONFIG_CHANGE,
  483. "audit_pid=%d old=%d by auid=%u subj=%s",
  484. status_get->pid, old,
  485. loginuid, ctx);
  486. kfree(ctx);
  487. } else
  488. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  489. "audit_pid=%d old=%d by auid=%u",
  490. status_get->pid, old, loginuid);
  491. audit_pid = status_get->pid;
  492. }
  493. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  494. err = audit_set_rate_limit(status_get->rate_limit,
  495. loginuid, sid);
  496. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  497. err = audit_set_backlog_limit(status_get->backlog_limit,
  498. loginuid, sid);
  499. break;
  500. case AUDIT_USER:
  501. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  502. case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
  503. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  504. return 0;
  505. err = audit_filter_user(&NETLINK_CB(skb), msg_type);
  506. if (err == 1) {
  507. err = 0;
  508. ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  509. if (ab) {
  510. audit_log_format(ab,
  511. "user pid=%d uid=%u auid=%u",
  512. pid, uid, loginuid);
  513. if (sid) {
  514. if (selinux_sid_to_string(
  515. sid, &ctx, &len)) {
  516. audit_log_format(ab,
  517. " ssid=%u", sid);
  518. /* Maybe call audit_panic? */
  519. } else
  520. audit_log_format(ab,
  521. " subj=%s", ctx);
  522. kfree(ctx);
  523. }
  524. audit_log_format(ab, " msg='%.1024s'",
  525. (char *)data);
  526. audit_set_pid(ab, pid);
  527. audit_log_end(ab);
  528. }
  529. }
  530. break;
  531. case AUDIT_ADD:
  532. case AUDIT_DEL:
  533. if (nlmsg_len(nlh) < sizeof(struct audit_rule))
  534. return -EINVAL;
  535. /* fallthrough */
  536. case AUDIT_LIST:
  537. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  538. uid, seq, data, nlmsg_len(nlh),
  539. loginuid, sid);
  540. break;
  541. case AUDIT_ADD_RULE:
  542. case AUDIT_DEL_RULE:
  543. if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
  544. return -EINVAL;
  545. /* fallthrough */
  546. case AUDIT_LIST_RULES:
  547. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  548. uid, seq, data, nlmsg_len(nlh),
  549. loginuid, sid);
  550. break;
  551. case AUDIT_SIGNAL_INFO:
  552. err = selinux_sid_to_string(audit_sig_sid, &ctx, &len);
  553. if (err)
  554. return err;
  555. sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
  556. if (!sig_data) {
  557. kfree(ctx);
  558. return -ENOMEM;
  559. }
  560. sig_data->uid = audit_sig_uid;
  561. sig_data->pid = audit_sig_pid;
  562. memcpy(sig_data->ctx, ctx, len);
  563. kfree(ctx);
  564. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  565. 0, 0, sig_data, sizeof(*sig_data) + len);
  566. kfree(sig_data);
  567. break;
  568. default:
  569. err = -EINVAL;
  570. break;
  571. }
  572. return err < 0 ? err : 0;
  573. }
  574. /*
  575. * Get message from skb (based on rtnetlink_rcv_skb). Each message is
  576. * processed by audit_receive_msg. Malformed skbs with wrong length are
  577. * discarded silently.
  578. */
  579. static void audit_receive_skb(struct sk_buff *skb)
  580. {
  581. int err;
  582. struct nlmsghdr *nlh;
  583. u32 rlen;
  584. while (skb->len >= NLMSG_SPACE(0)) {
  585. nlh = (struct nlmsghdr *)skb->data;
  586. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  587. return;
  588. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  589. if (rlen > skb->len)
  590. rlen = skb->len;
  591. if ((err = audit_receive_msg(skb, nlh))) {
  592. netlink_ack(skb, nlh, err);
  593. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  594. netlink_ack(skb, nlh, 0);
  595. skb_pull(skb, rlen);
  596. }
  597. }
  598. /* Receive messages from netlink socket. */
  599. static void audit_receive(struct sock *sk, int length)
  600. {
  601. struct sk_buff *skb;
  602. unsigned int qlen;
  603. mutex_lock(&audit_cmd_mutex);
  604. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  605. skb = skb_dequeue(&sk->sk_receive_queue);
  606. audit_receive_skb(skb);
  607. kfree_skb(skb);
  608. }
  609. mutex_unlock(&audit_cmd_mutex);
  610. }
  611. #ifdef CONFIG_AUDITSYSCALL
  612. static const struct inotify_operations audit_inotify_ops = {
  613. .handle_event = audit_handle_ievent,
  614. .destroy_watch = audit_free_parent,
  615. };
  616. #endif
  617. /* Initialize audit support at boot time. */
  618. static int __init audit_init(void)
  619. {
  620. int i;
  621. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  622. audit_default ? "enabled" : "disabled");
  623. audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
  624. THIS_MODULE);
  625. if (!audit_sock)
  626. audit_panic("cannot initialize netlink socket");
  627. else
  628. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  629. skb_queue_head_init(&audit_skb_queue);
  630. audit_initialized = 1;
  631. audit_enabled = audit_default;
  632. /* Register the callback with selinux. This callback will be invoked
  633. * when a new policy is loaded. */
  634. selinux_audit_set_callback(&selinux_audit_rule_update);
  635. audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
  636. #ifdef CONFIG_AUDITSYSCALL
  637. audit_ih = inotify_init(&audit_inotify_ops);
  638. if (IS_ERR(audit_ih))
  639. audit_panic("cannot initialize inotify handle");
  640. #endif
  641. for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
  642. INIT_LIST_HEAD(&audit_inode_hash[i]);
  643. return 0;
  644. }
  645. __initcall(audit_init);
  646. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  647. static int __init audit_enable(char *str)
  648. {
  649. audit_default = !!simple_strtol(str, NULL, 0);
  650. printk(KERN_INFO "audit: %s%s\n",
  651. audit_default ? "enabled" : "disabled",
  652. audit_initialized ? "" : " (after initialization)");
  653. if (audit_initialized)
  654. audit_enabled = audit_default;
  655. return 1;
  656. }
  657. __setup("audit=", audit_enable);
  658. static void audit_buffer_free(struct audit_buffer *ab)
  659. {
  660. unsigned long flags;
  661. if (!ab)
  662. return;
  663. if (ab->skb)
  664. kfree_skb(ab->skb);
  665. spin_lock_irqsave(&audit_freelist_lock, flags);
  666. if (audit_freelist_count > AUDIT_MAXFREE)
  667. kfree(ab);
  668. else {
  669. audit_freelist_count++;
  670. list_add(&ab->list, &audit_freelist);
  671. }
  672. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  673. }
  674. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  675. gfp_t gfp_mask, int type)
  676. {
  677. unsigned long flags;
  678. struct audit_buffer *ab = NULL;
  679. struct nlmsghdr *nlh;
  680. spin_lock_irqsave(&audit_freelist_lock, flags);
  681. if (!list_empty(&audit_freelist)) {
  682. ab = list_entry(audit_freelist.next,
  683. struct audit_buffer, list);
  684. list_del(&ab->list);
  685. --audit_freelist_count;
  686. }
  687. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  688. if (!ab) {
  689. ab = kmalloc(sizeof(*ab), gfp_mask);
  690. if (!ab)
  691. goto err;
  692. }
  693. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  694. if (!ab->skb)
  695. goto err;
  696. ab->ctx = ctx;
  697. ab->gfp_mask = gfp_mask;
  698. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  699. nlh->nlmsg_type = type;
  700. nlh->nlmsg_flags = 0;
  701. nlh->nlmsg_pid = 0;
  702. nlh->nlmsg_seq = 0;
  703. return ab;
  704. err:
  705. audit_buffer_free(ab);
  706. return NULL;
  707. }
  708. /**
  709. * audit_serial - compute a serial number for the audit record
  710. *
  711. * Compute a serial number for the audit record. Audit records are
  712. * written to user-space as soon as they are generated, so a complete
  713. * audit record may be written in several pieces. The timestamp of the
  714. * record and this serial number are used by the user-space tools to
  715. * determine which pieces belong to the same audit record. The
  716. * (timestamp,serial) tuple is unique for each syscall and is live from
  717. * syscall entry to syscall exit.
  718. *
  719. * NOTE: Another possibility is to store the formatted records off the
  720. * audit context (for those records that have a context), and emit them
  721. * all at syscall exit. However, this could delay the reporting of
  722. * significant errors until syscall exit (or never, if the system
  723. * halts).
  724. */
  725. unsigned int audit_serial(void)
  726. {
  727. static DEFINE_SPINLOCK(serial_lock);
  728. static unsigned int serial = 0;
  729. unsigned long flags;
  730. unsigned int ret;
  731. spin_lock_irqsave(&serial_lock, flags);
  732. do {
  733. ret = ++serial;
  734. } while (unlikely(!ret));
  735. spin_unlock_irqrestore(&serial_lock, flags);
  736. return ret;
  737. }
  738. static inline void audit_get_stamp(struct audit_context *ctx,
  739. struct timespec *t, unsigned int *serial)
  740. {
  741. if (ctx)
  742. auditsc_get_stamp(ctx, t, serial);
  743. else {
  744. *t = CURRENT_TIME;
  745. *serial = audit_serial();
  746. }
  747. }
  748. /* Obtain an audit buffer. This routine does locking to obtain the
  749. * audit buffer, but then no locking is required for calls to
  750. * audit_log_*format. If the tsk is a task that is currently in a
  751. * syscall, then the syscall is marked as auditable and an audit record
  752. * will be written at syscall exit. If there is no associated task, tsk
  753. * should be NULL. */
  754. /**
  755. * audit_log_start - obtain an audit buffer
  756. * @ctx: audit_context (may be NULL)
  757. * @gfp_mask: type of allocation
  758. * @type: audit message type
  759. *
  760. * Returns audit_buffer pointer on success or NULL on error.
  761. *
  762. * Obtain an audit buffer. This routine does locking to obtain the
  763. * audit buffer, but then no locking is required for calls to
  764. * audit_log_*format. If the task (ctx) is a task that is currently in a
  765. * syscall, then the syscall is marked as auditable and an audit record
  766. * will be written at syscall exit. If there is no associated task, then
  767. * task context (ctx) should be NULL.
  768. */
  769. struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
  770. int type)
  771. {
  772. struct audit_buffer *ab = NULL;
  773. struct timespec t;
  774. unsigned int serial;
  775. int reserve;
  776. unsigned long timeout_start = jiffies;
  777. if (!audit_initialized)
  778. return NULL;
  779. if (unlikely(audit_filter_type(type)))
  780. return NULL;
  781. if (gfp_mask & __GFP_WAIT)
  782. reserve = 0;
  783. else
  784. reserve = 5; /* Allow atomic callers to go up to five
  785. entries over the normal backlog limit */
  786. while (audit_backlog_limit
  787. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
  788. if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
  789. && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
  790. /* Wait for auditd to drain the queue a little */
  791. DECLARE_WAITQUEUE(wait, current);
  792. set_current_state(TASK_INTERRUPTIBLE);
  793. add_wait_queue(&audit_backlog_wait, &wait);
  794. if (audit_backlog_limit &&
  795. skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
  796. schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
  797. __set_current_state(TASK_RUNNING);
  798. remove_wait_queue(&audit_backlog_wait, &wait);
  799. continue;
  800. }
  801. if (audit_rate_check())
  802. printk(KERN_WARNING
  803. "audit: audit_backlog=%d > "
  804. "audit_backlog_limit=%d\n",
  805. skb_queue_len(&audit_skb_queue),
  806. audit_backlog_limit);
  807. audit_log_lost("backlog limit exceeded");
  808. audit_backlog_wait_time = audit_backlog_wait_overflow;
  809. wake_up(&audit_backlog_wait);
  810. return NULL;
  811. }
  812. ab = audit_buffer_alloc(ctx, gfp_mask, type);
  813. if (!ab) {
  814. audit_log_lost("out of memory in audit_log_start");
  815. return NULL;
  816. }
  817. audit_get_stamp(ab->ctx, &t, &serial);
  818. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  819. t.tv_sec, t.tv_nsec/1000000, serial);
  820. return ab;
  821. }
  822. /**
  823. * audit_expand - expand skb in the audit buffer
  824. * @ab: audit_buffer
  825. * @extra: space to add at tail of the skb
  826. *
  827. * Returns 0 (no space) on failed expansion, or available space if
  828. * successful.
  829. */
  830. static inline int audit_expand(struct audit_buffer *ab, int extra)
  831. {
  832. struct sk_buff *skb = ab->skb;
  833. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  834. ab->gfp_mask);
  835. if (ret < 0) {
  836. audit_log_lost("out of memory in audit_expand");
  837. return 0;
  838. }
  839. return skb_tailroom(skb);
  840. }
  841. /*
  842. * Format an audit message into the audit buffer. If there isn't enough
  843. * room in the audit buffer, more room will be allocated and vsnprint
  844. * will be called a second time. Currently, we assume that a printk
  845. * can't format message larger than 1024 bytes, so we don't either.
  846. */
  847. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  848. va_list args)
  849. {
  850. int len, avail;
  851. struct sk_buff *skb;
  852. va_list args2;
  853. if (!ab)
  854. return;
  855. BUG_ON(!ab->skb);
  856. skb = ab->skb;
  857. avail = skb_tailroom(skb);
  858. if (avail == 0) {
  859. avail = audit_expand(ab, AUDIT_BUFSIZ);
  860. if (!avail)
  861. goto out;
  862. }
  863. va_copy(args2, args);
  864. len = vsnprintf(skb->tail, avail, fmt, args);
  865. if (len >= avail) {
  866. /* The printk buffer is 1024 bytes long, so if we get
  867. * here and AUDIT_BUFSIZ is at least 1024, then we can
  868. * log everything that printk could have logged. */
  869. avail = audit_expand(ab,
  870. max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  871. if (!avail)
  872. goto out;
  873. len = vsnprintf(skb->tail, avail, fmt, args2);
  874. }
  875. if (len > 0)
  876. skb_put(skb, len);
  877. out:
  878. return;
  879. }
  880. /**
  881. * audit_log_format - format a message into the audit buffer.
  882. * @ab: audit_buffer
  883. * @fmt: format string
  884. * @...: optional parameters matching @fmt string
  885. *
  886. * All the work is done in audit_log_vformat.
  887. */
  888. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  889. {
  890. va_list args;
  891. if (!ab)
  892. return;
  893. va_start(args, fmt);
  894. audit_log_vformat(ab, fmt, args);
  895. va_end(args);
  896. }
  897. /**
  898. * audit_log_hex - convert a buffer to hex and append it to the audit skb
  899. * @ab: the audit_buffer
  900. * @buf: buffer to convert to hex
  901. * @len: length of @buf to be converted
  902. *
  903. * No return value; failure to expand is silently ignored.
  904. *
  905. * This function will take the passed buf and convert it into a string of
  906. * ascii hex digits. The new string is placed onto the skb.
  907. */
  908. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  909. size_t len)
  910. {
  911. int i, avail, new_len;
  912. unsigned char *ptr;
  913. struct sk_buff *skb;
  914. static const unsigned char *hex = "0123456789ABCDEF";
  915. if (!ab)
  916. return;
  917. BUG_ON(!ab->skb);
  918. skb = ab->skb;
  919. avail = skb_tailroom(skb);
  920. new_len = len<<1;
  921. if (new_len >= avail) {
  922. /* Round the buffer request up to the next multiple */
  923. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  924. avail = audit_expand(ab, new_len);
  925. if (!avail)
  926. return;
  927. }
  928. ptr = skb->tail;
  929. for (i=0; i<len; i++) {
  930. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  931. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  932. }
  933. *ptr = 0;
  934. skb_put(skb, len << 1); /* new string is twice the old string */
  935. }
  936. /*
  937. * Format a string of no more than slen characters into the audit buffer,
  938. * enclosed in quote marks.
  939. */
  940. static void audit_log_n_string(struct audit_buffer *ab, size_t slen,
  941. const char *string)
  942. {
  943. int avail, new_len;
  944. unsigned char *ptr;
  945. struct sk_buff *skb;
  946. if (!ab)
  947. return;
  948. BUG_ON(!ab->skb);
  949. skb = ab->skb;
  950. avail = skb_tailroom(skb);
  951. new_len = slen + 3; /* enclosing quotes + null terminator */
  952. if (new_len > avail) {
  953. avail = audit_expand(ab, new_len);
  954. if (!avail)
  955. return;
  956. }
  957. ptr = skb->tail;
  958. *ptr++ = '"';
  959. memcpy(ptr, string, slen);
  960. ptr += slen;
  961. *ptr++ = '"';
  962. *ptr = 0;
  963. skb_put(skb, slen + 2); /* don't include null terminator */
  964. }
  965. /**
  966. * audit_log_n_unstrustedstring - log a string that may contain random characters
  967. * @ab: audit_buffer
  968. * @len: lenth of string (not including trailing null)
  969. * @string: string to be logged
  970. *
  971. * This code will escape a string that is passed to it if the string
  972. * contains a control character, unprintable character, double quote mark,
  973. * or a space. Unescaped strings will start and end with a double quote mark.
  974. * Strings that are escaped are printed in hex (2 digits per char).
  975. *
  976. * The caller specifies the number of characters in the string to log, which may
  977. * or may not be the entire string.
  978. */
  979. const char *audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len,
  980. const char *string)
  981. {
  982. const unsigned char *p = string;
  983. while (*p) {
  984. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  985. audit_log_hex(ab, string, len);
  986. return string + len + 1;
  987. }
  988. p++;
  989. }
  990. audit_log_n_string(ab, len, string);
  991. return p + 1;
  992. }
  993. /**
  994. * audit_log_unstrustedstring - log a string that may contain random characters
  995. * @ab: audit_buffer
  996. * @string: string to be logged
  997. *
  998. * Same as audit_log_n_unstrustedstring(), except that strlen is used to
  999. * determine string length.
  1000. */
  1001. const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  1002. {
  1003. return audit_log_n_untrustedstring(ab, strlen(string), string);
  1004. }
  1005. /* This is a helper-function to print the escaped d_path */
  1006. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  1007. struct dentry *dentry, struct vfsmount *vfsmnt)
  1008. {
  1009. char *p, *path;
  1010. if (prefix)
  1011. audit_log_format(ab, " %s", prefix);
  1012. /* We will allow 11 spaces for ' (deleted)' to be appended */
  1013. path = kmalloc(PATH_MAX+11, ab->gfp_mask);
  1014. if (!path) {
  1015. audit_log_format(ab, "<no memory>");
  1016. return;
  1017. }
  1018. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  1019. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  1020. /* FIXME: can we save some information here? */
  1021. audit_log_format(ab, "<too long>");
  1022. } else
  1023. audit_log_untrustedstring(ab, p);
  1024. kfree(path);
  1025. }
  1026. /**
  1027. * audit_log_end - end one audit record
  1028. * @ab: the audit_buffer
  1029. *
  1030. * The netlink_* functions cannot be called inside an irq context, so
  1031. * the audit buffer is placed on a queue and a tasklet is scheduled to
  1032. * remove them from the queue outside the irq context. May be called in
  1033. * any context.
  1034. */
  1035. void audit_log_end(struct audit_buffer *ab)
  1036. {
  1037. if (!ab)
  1038. return;
  1039. if (!audit_rate_check()) {
  1040. audit_log_lost("rate limit exceeded");
  1041. } else {
  1042. if (audit_pid) {
  1043. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  1044. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  1045. skb_queue_tail(&audit_skb_queue, ab->skb);
  1046. ab->skb = NULL;
  1047. wake_up_interruptible(&kauditd_wait);
  1048. } else {
  1049. printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
  1050. }
  1051. }
  1052. audit_buffer_free(ab);
  1053. }
  1054. /**
  1055. * audit_log - Log an audit record
  1056. * @ctx: audit context
  1057. * @gfp_mask: type of allocation
  1058. * @type: audit message type
  1059. * @fmt: format string to use
  1060. * @...: variable parameters matching the format string
  1061. *
  1062. * This is a convenience function that calls audit_log_start,
  1063. * audit_log_vformat, and audit_log_end. It may be called
  1064. * in any context.
  1065. */
  1066. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  1067. const char *fmt, ...)
  1068. {
  1069. struct audit_buffer *ab;
  1070. va_list args;
  1071. ab = audit_log_start(ctx, gfp_mask, type);
  1072. if (ab) {
  1073. va_start(args, fmt);
  1074. audit_log_vformat(ab, fmt, args);
  1075. va_end(args);
  1076. audit_log_end(ab);
  1077. }
  1078. }
  1079. EXPORT_SYMBOL(audit_log_start);
  1080. EXPORT_SYMBOL(audit_log_end);
  1081. EXPORT_SYMBOL(audit_log_format);
  1082. EXPORT_SYMBOL(audit_log);