audit.c 36 KB

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