audit.c 40 KB

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