audit.c 36 KB

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