auditsc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /* auditsc.c -- System-call auditing support
  2. * Handles all system-call specific auditing features.
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  22. *
  23. * Many of the ideas implemented here are from Stephen C. Tweedie,
  24. * especially the idea of avoiding a copy by using getname.
  25. *
  26. * The method for actual interception of syscall entry and exit (not in
  27. * this file -- see entry.S) is based on a GPL'd patch written by
  28. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <asm/atomic.h>
  33. #include <asm/types.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/mount.h>
  37. #include <linux/socket.h>
  38. #include <linux/audit.h>
  39. #include <linux/personality.h>
  40. #include <linux/time.h>
  41. #include <linux/kthread.h>
  42. #include <linux/netlink.h>
  43. #include <linux/compiler.h>
  44. #include <asm/unistd.h>
  45. /* 0 = no checking
  46. 1 = put_count checking
  47. 2 = verbose put_count checking
  48. */
  49. #define AUDIT_DEBUG 0
  50. /* No syscall auditing will take place unless audit_enabled != 0. */
  51. extern int audit_enabled;
  52. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  53. * for saving names from getname(). */
  54. #define AUDIT_NAMES 20
  55. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  56. * audit_context from being used for nameless inodes from
  57. * path_lookup. */
  58. #define AUDIT_NAMES_RESERVED 7
  59. /* At task start time, the audit_state is set in the audit_context using
  60. a per-task filter. At syscall entry, the audit_state is augmented by
  61. the syscall filter. */
  62. enum audit_state {
  63. AUDIT_DISABLED, /* Do not create per-task audit_context.
  64. * No syscall-specific audit records can
  65. * be generated. */
  66. AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
  67. * but don't necessarily fill it in at
  68. * syscall entry time (i.e., filter
  69. * instead). */
  70. AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
  71. * and always fill it in at syscall
  72. * entry time. This makes a full
  73. * syscall record available if some
  74. * other part of the kernel decides it
  75. * should be recorded. */
  76. AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
  77. * always fill it in at syscall entry
  78. * time, and always write out the audit
  79. * record at syscall exit time. */
  80. };
  81. /* When fs/namei.c:getname() is called, we store the pointer in name and
  82. * we don't let putname() free it (instead we free all of the saved
  83. * pointers at syscall exit time).
  84. *
  85. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  86. struct audit_names {
  87. const char *name;
  88. unsigned long ino;
  89. dev_t dev;
  90. umode_t mode;
  91. uid_t uid;
  92. gid_t gid;
  93. dev_t rdev;
  94. unsigned flags;
  95. };
  96. struct audit_aux_data {
  97. struct audit_aux_data *next;
  98. int type;
  99. };
  100. #define AUDIT_AUX_IPCPERM 0
  101. struct audit_aux_data_ipcctl {
  102. struct audit_aux_data d;
  103. struct ipc_perm p;
  104. unsigned long qbytes;
  105. uid_t uid;
  106. gid_t gid;
  107. mode_t mode;
  108. };
  109. struct audit_aux_data_socketcall {
  110. struct audit_aux_data d;
  111. int nargs;
  112. unsigned long args[0];
  113. };
  114. struct audit_aux_data_sockaddr {
  115. struct audit_aux_data d;
  116. int len;
  117. char a[0];
  118. };
  119. struct audit_aux_data_path {
  120. struct audit_aux_data d;
  121. struct dentry *dentry;
  122. struct vfsmount *mnt;
  123. };
  124. /* The per-task audit context. */
  125. struct audit_context {
  126. int in_syscall; /* 1 if task is in a syscall */
  127. enum audit_state state;
  128. unsigned int serial; /* serial number for record */
  129. struct timespec ctime; /* time of syscall entry */
  130. uid_t loginuid; /* login uid (identity) */
  131. int major; /* syscall number */
  132. unsigned long argv[4]; /* syscall arguments */
  133. int return_valid; /* return code is valid */
  134. long return_code;/* syscall return code */
  135. int auditable; /* 1 if record should be written */
  136. int name_count;
  137. struct audit_names names[AUDIT_NAMES];
  138. struct dentry * pwd;
  139. struct vfsmount * pwdmnt;
  140. struct audit_context *previous; /* For nested syscalls */
  141. struct audit_aux_data *aux;
  142. /* Save things to print about task_struct */
  143. pid_t pid;
  144. uid_t uid, euid, suid, fsuid;
  145. gid_t gid, egid, sgid, fsgid;
  146. unsigned long personality;
  147. int arch;
  148. #if AUDIT_DEBUG
  149. int put_count;
  150. int ino_count;
  151. #endif
  152. };
  153. /* Public API */
  154. /* There are three lists of rules -- one to search at task creation
  155. * time, one to search at syscall entry time, and another to search at
  156. * syscall exit time. */
  157. static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  158. LIST_HEAD_INIT(audit_filter_list[0]),
  159. LIST_HEAD_INIT(audit_filter_list[1]),
  160. LIST_HEAD_INIT(audit_filter_list[2]),
  161. LIST_HEAD_INIT(audit_filter_list[3]),
  162. LIST_HEAD_INIT(audit_filter_list[4]),
  163. #if AUDIT_NR_FILTERS != 5
  164. #error Fix audit_filter_list initialiser
  165. #endif
  166. };
  167. struct audit_entry {
  168. struct list_head list;
  169. struct rcu_head rcu;
  170. struct audit_rule rule;
  171. };
  172. extern int audit_pid;
  173. /* Check to see if two rules are identical. It is called from
  174. * audit_del_rule during AUDIT_DEL. */
  175. static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
  176. {
  177. int i;
  178. if (a->flags != b->flags)
  179. return 1;
  180. if (a->action != b->action)
  181. return 1;
  182. if (a->field_count != b->field_count)
  183. return 1;
  184. for (i = 0; i < a->field_count; i++) {
  185. if (a->fields[i] != b->fields[i]
  186. || a->values[i] != b->values[i])
  187. return 1;
  188. }
  189. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  190. if (a->mask[i] != b->mask[i])
  191. return 1;
  192. return 0;
  193. }
  194. /* Note that audit_add_rule and audit_del_rule are called via
  195. * audit_receive() in audit.c, and are protected by
  196. * audit_netlink_sem. */
  197. static inline void audit_add_rule(struct audit_entry *entry,
  198. struct list_head *list)
  199. {
  200. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  201. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  202. list_add_rcu(&entry->list, list);
  203. } else {
  204. list_add_tail_rcu(&entry->list, list);
  205. }
  206. }
  207. static void audit_free_rule(struct rcu_head *head)
  208. {
  209. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  210. kfree(e);
  211. }
  212. /* Note that audit_add_rule and audit_del_rule are called via
  213. * audit_receive() in audit.c, and are protected by
  214. * audit_netlink_sem. */
  215. static inline int audit_del_rule(struct audit_rule *rule,
  216. struct list_head *list)
  217. {
  218. struct audit_entry *e;
  219. /* Do not use the _rcu iterator here, since this is the only
  220. * deletion routine. */
  221. list_for_each_entry(e, list, list) {
  222. if (!audit_compare_rule(rule, &e->rule)) {
  223. list_del_rcu(&e->list);
  224. call_rcu(&e->rcu, audit_free_rule);
  225. return 0;
  226. }
  227. }
  228. return -ENOENT; /* No matching rule */
  229. }
  230. /* Copy rule from user-space to kernel-space. Called during
  231. * AUDIT_ADD. */
  232. static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  233. {
  234. int i;
  235. if (s->action != AUDIT_NEVER
  236. && s->action != AUDIT_POSSIBLE
  237. && s->action != AUDIT_ALWAYS)
  238. return -1;
  239. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  240. return -1;
  241. if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
  242. return -1;
  243. d->flags = s->flags;
  244. d->action = s->action;
  245. d->field_count = s->field_count;
  246. for (i = 0; i < d->field_count; i++) {
  247. d->fields[i] = s->fields[i];
  248. d->values[i] = s->values[i];
  249. }
  250. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  251. return 0;
  252. }
  253. static int audit_list_rules(void *_dest)
  254. {
  255. int pid, seq;
  256. int *dest = _dest;
  257. struct audit_entry *entry;
  258. int i;
  259. pid = dest[0];
  260. seq = dest[1];
  261. kfree(dest);
  262. down(&audit_netlink_sem);
  263. /* The *_rcu iterators not needed here because we are
  264. always called with audit_netlink_sem held. */
  265. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  266. list_for_each_entry(entry, &audit_filter_list[i], list)
  267. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  268. &entry->rule, sizeof(entry->rule));
  269. }
  270. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  271. up(&audit_netlink_sem);
  272. return 0;
  273. }
  274. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  275. uid_t loginuid)
  276. {
  277. struct audit_entry *entry;
  278. struct task_struct *tsk;
  279. int *dest;
  280. int err = 0;
  281. unsigned listnr;
  282. switch (type) {
  283. case AUDIT_LIST:
  284. /* We can't just spew out the rules here because we might fill
  285. * the available socket buffer space and deadlock waiting for
  286. * auditctl to read from it... which isn't ever going to
  287. * happen if we're actually running in the context of auditctl
  288. * trying to _send_ the stuff */
  289. dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
  290. if (!dest)
  291. return -ENOMEM;
  292. dest[0] = pid;
  293. dest[1] = seq;
  294. tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
  295. if (IS_ERR(tsk)) {
  296. kfree(dest);
  297. err = PTR_ERR(tsk);
  298. }
  299. break;
  300. case AUDIT_ADD:
  301. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  302. return -ENOMEM;
  303. if (audit_copy_rule(&entry->rule, data)) {
  304. kfree(entry);
  305. return -EINVAL;
  306. }
  307. listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
  308. audit_add_rule(entry, &audit_filter_list[listnr]);
  309. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  310. "auid=%u added an audit rule\n", loginuid);
  311. break;
  312. case AUDIT_DEL:
  313. listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
  314. if (listnr >= AUDIT_NR_FILTERS)
  315. return -EINVAL;
  316. err = audit_del_rule(data, &audit_filter_list[listnr]);
  317. if (!err)
  318. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  319. "auid=%u removed an audit rule\n", loginuid);
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. return err;
  325. }
  326. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  327. * otherwise. */
  328. static int audit_filter_rules(struct task_struct *tsk,
  329. struct audit_rule *rule,
  330. struct audit_context *ctx,
  331. enum audit_state *state)
  332. {
  333. int i, j;
  334. for (i = 0; i < rule->field_count; i++) {
  335. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  336. u32 value = rule->values[i];
  337. int result = 0;
  338. switch (field) {
  339. case AUDIT_PID:
  340. result = (tsk->pid == value);
  341. break;
  342. case AUDIT_UID:
  343. result = (tsk->uid == value);
  344. break;
  345. case AUDIT_EUID:
  346. result = (tsk->euid == value);
  347. break;
  348. case AUDIT_SUID:
  349. result = (tsk->suid == value);
  350. break;
  351. case AUDIT_FSUID:
  352. result = (tsk->fsuid == value);
  353. break;
  354. case AUDIT_GID:
  355. result = (tsk->gid == value);
  356. break;
  357. case AUDIT_EGID:
  358. result = (tsk->egid == value);
  359. break;
  360. case AUDIT_SGID:
  361. result = (tsk->sgid == value);
  362. break;
  363. case AUDIT_FSGID:
  364. result = (tsk->fsgid == value);
  365. break;
  366. case AUDIT_PERS:
  367. result = (tsk->personality == value);
  368. break;
  369. case AUDIT_ARCH:
  370. if (ctx)
  371. result = (ctx->arch == value);
  372. break;
  373. case AUDIT_EXIT:
  374. if (ctx && ctx->return_valid)
  375. result = (ctx->return_code == value);
  376. break;
  377. case AUDIT_SUCCESS:
  378. if (ctx && ctx->return_valid)
  379. result = (ctx->return_valid == AUDITSC_SUCCESS);
  380. break;
  381. case AUDIT_DEVMAJOR:
  382. if (ctx) {
  383. for (j = 0; j < ctx->name_count; j++) {
  384. if (MAJOR(ctx->names[j].dev)==value) {
  385. ++result;
  386. break;
  387. }
  388. }
  389. }
  390. break;
  391. case AUDIT_DEVMINOR:
  392. if (ctx) {
  393. for (j = 0; j < ctx->name_count; j++) {
  394. if (MINOR(ctx->names[j].dev)==value) {
  395. ++result;
  396. break;
  397. }
  398. }
  399. }
  400. break;
  401. case AUDIT_INODE:
  402. if (ctx) {
  403. for (j = 0; j < ctx->name_count; j++) {
  404. if (ctx->names[j].ino == value) {
  405. ++result;
  406. break;
  407. }
  408. }
  409. }
  410. break;
  411. case AUDIT_LOGINUID:
  412. result = 0;
  413. if (ctx)
  414. result = (ctx->loginuid == value);
  415. break;
  416. case AUDIT_ARG0:
  417. case AUDIT_ARG1:
  418. case AUDIT_ARG2:
  419. case AUDIT_ARG3:
  420. if (ctx)
  421. result = (ctx->argv[field-AUDIT_ARG0]==value);
  422. break;
  423. }
  424. if (rule->fields[i] & AUDIT_NEGATE)
  425. result = !result;
  426. if (!result)
  427. return 0;
  428. }
  429. switch (rule->action) {
  430. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  431. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  432. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  433. }
  434. return 1;
  435. }
  436. /* At process creation time, we can determine if system-call auditing is
  437. * completely disabled for this task. Since we only have the task
  438. * structure at this point, we can only check uid and gid.
  439. */
  440. static enum audit_state audit_filter_task(struct task_struct *tsk)
  441. {
  442. struct audit_entry *e;
  443. enum audit_state state;
  444. rcu_read_lock();
  445. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  446. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  447. rcu_read_unlock();
  448. return state;
  449. }
  450. }
  451. rcu_read_unlock();
  452. return AUDIT_BUILD_CONTEXT;
  453. }
  454. /* At syscall entry and exit time, this filter is called if the
  455. * audit_state is not low enough that auditing cannot take place, but is
  456. * also not high enough that we already know we have to write an audit
  457. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  458. */
  459. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  460. struct audit_context *ctx,
  461. struct list_head *list)
  462. {
  463. struct audit_entry *e;
  464. enum audit_state state;
  465. int word = AUDIT_WORD(ctx->major);
  466. int bit = AUDIT_BIT(ctx->major);
  467. if (audit_pid && tsk->tgid == audit_pid)
  468. return AUDIT_DISABLED;
  469. rcu_read_lock();
  470. list_for_each_entry_rcu(e, list, list) {
  471. if ((e->rule.mask[word] & bit) == bit
  472. && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
  473. rcu_read_unlock();
  474. return state;
  475. }
  476. }
  477. rcu_read_unlock();
  478. return AUDIT_BUILD_CONTEXT;
  479. }
  480. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  481. struct audit_rule *rule,
  482. enum audit_state *state)
  483. {
  484. int i;
  485. for (i = 0; i < rule->field_count; i++) {
  486. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  487. u32 value = rule->values[i];
  488. int result = 0;
  489. switch (field) {
  490. case AUDIT_PID:
  491. result = (cb->creds.pid == value);
  492. break;
  493. case AUDIT_UID:
  494. result = (cb->creds.uid == value);
  495. break;
  496. case AUDIT_GID:
  497. result = (cb->creds.gid == value);
  498. break;
  499. case AUDIT_LOGINUID:
  500. result = (cb->loginuid == value);
  501. break;
  502. }
  503. if (rule->fields[i] & AUDIT_NEGATE)
  504. result = !result;
  505. if (!result)
  506. return 0;
  507. }
  508. switch (rule->action) {
  509. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  510. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  511. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  512. }
  513. return 1;
  514. }
  515. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  516. {
  517. struct audit_entry *e;
  518. enum audit_state state;
  519. int ret = 1;
  520. rcu_read_lock();
  521. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  522. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  523. if (state == AUDIT_DISABLED)
  524. ret = 0;
  525. break;
  526. }
  527. }
  528. rcu_read_unlock();
  529. return ret; /* Audit by default */
  530. }
  531. /* This should be called with task_lock() held. */
  532. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  533. int return_valid,
  534. int return_code)
  535. {
  536. struct audit_context *context = tsk->audit_context;
  537. if (likely(!context))
  538. return NULL;
  539. context->return_valid = return_valid;
  540. context->return_code = return_code;
  541. if (context->in_syscall && !context->auditable) {
  542. enum audit_state state;
  543. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  544. if (state == AUDIT_RECORD_CONTEXT)
  545. context->auditable = 1;
  546. }
  547. context->pid = tsk->pid;
  548. context->uid = tsk->uid;
  549. context->gid = tsk->gid;
  550. context->euid = tsk->euid;
  551. context->suid = tsk->suid;
  552. context->fsuid = tsk->fsuid;
  553. context->egid = tsk->egid;
  554. context->sgid = tsk->sgid;
  555. context->fsgid = tsk->fsgid;
  556. context->personality = tsk->personality;
  557. tsk->audit_context = NULL;
  558. return context;
  559. }
  560. static inline void audit_free_names(struct audit_context *context)
  561. {
  562. int i;
  563. #if AUDIT_DEBUG == 2
  564. if (context->auditable
  565. ||context->put_count + context->ino_count != context->name_count) {
  566. printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
  567. " name_count=%d put_count=%d"
  568. " ino_count=%d [NOT freeing]\n",
  569. __LINE__,
  570. context->serial, context->major, context->in_syscall,
  571. context->name_count, context->put_count,
  572. context->ino_count);
  573. for (i = 0; i < context->name_count; i++)
  574. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  575. context->names[i].name,
  576. context->names[i].name);
  577. dump_stack();
  578. return;
  579. }
  580. #endif
  581. #if AUDIT_DEBUG
  582. context->put_count = 0;
  583. context->ino_count = 0;
  584. #endif
  585. for (i = 0; i < context->name_count; i++)
  586. if (context->names[i].name)
  587. __putname(context->names[i].name);
  588. context->name_count = 0;
  589. if (context->pwd)
  590. dput(context->pwd);
  591. if (context->pwdmnt)
  592. mntput(context->pwdmnt);
  593. context->pwd = NULL;
  594. context->pwdmnt = NULL;
  595. }
  596. static inline void audit_free_aux(struct audit_context *context)
  597. {
  598. struct audit_aux_data *aux;
  599. while ((aux = context->aux)) {
  600. if (aux->type == AUDIT_AVC_PATH) {
  601. struct audit_aux_data_path *axi = (void *)aux;
  602. dput(axi->dentry);
  603. mntput(axi->mnt);
  604. }
  605. context->aux = aux->next;
  606. kfree(aux);
  607. }
  608. }
  609. static inline void audit_zero_context(struct audit_context *context,
  610. enum audit_state state)
  611. {
  612. uid_t loginuid = context->loginuid;
  613. memset(context, 0, sizeof(*context));
  614. context->state = state;
  615. context->loginuid = loginuid;
  616. }
  617. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  618. {
  619. struct audit_context *context;
  620. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  621. return NULL;
  622. audit_zero_context(context, state);
  623. return context;
  624. }
  625. /* Filter on the task information and allocate a per-task audit context
  626. * if necessary. Doing so turns on system call auditing for the
  627. * specified task. This is called from copy_process, so no lock is
  628. * needed. */
  629. int audit_alloc(struct task_struct *tsk)
  630. {
  631. struct audit_context *context;
  632. enum audit_state state;
  633. if (likely(!audit_enabled))
  634. return 0; /* Return if not auditing. */
  635. state = audit_filter_task(tsk);
  636. if (likely(state == AUDIT_DISABLED))
  637. return 0;
  638. if (!(context = audit_alloc_context(state))) {
  639. audit_log_lost("out of memory in audit_alloc");
  640. return -ENOMEM;
  641. }
  642. /* Preserve login uid */
  643. context->loginuid = -1;
  644. if (current->audit_context)
  645. context->loginuid = current->audit_context->loginuid;
  646. tsk->audit_context = context;
  647. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  648. return 0;
  649. }
  650. static inline void audit_free_context(struct audit_context *context)
  651. {
  652. struct audit_context *previous;
  653. int count = 0;
  654. do {
  655. previous = context->previous;
  656. if (previous || (count && count < 10)) {
  657. ++count;
  658. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  659. " freeing multiple contexts (%d)\n",
  660. context->serial, context->major,
  661. context->name_count, count);
  662. }
  663. audit_free_names(context);
  664. audit_free_aux(context);
  665. kfree(context);
  666. context = previous;
  667. } while (context);
  668. if (count >= 10)
  669. printk(KERN_ERR "audit: freed %d contexts\n", count);
  670. }
  671. static void audit_log_task_info(struct audit_buffer *ab)
  672. {
  673. char name[sizeof(current->comm)];
  674. struct mm_struct *mm = current->mm;
  675. struct vm_area_struct *vma;
  676. get_task_comm(name, current);
  677. audit_log_format(ab, " comm=");
  678. audit_log_untrustedstring(ab, name);
  679. if (!mm)
  680. return;
  681. down_read(&mm->mmap_sem);
  682. vma = mm->mmap;
  683. while (vma) {
  684. if ((vma->vm_flags & VM_EXECUTABLE) &&
  685. vma->vm_file) {
  686. audit_log_d_path(ab, "exe=",
  687. vma->vm_file->f_dentry,
  688. vma->vm_file->f_vfsmnt);
  689. break;
  690. }
  691. vma = vma->vm_next;
  692. }
  693. up_read(&mm->mmap_sem);
  694. }
  695. static void audit_log_exit(struct audit_context *context, unsigned int gfp_mask)
  696. {
  697. int i;
  698. struct audit_buffer *ab;
  699. struct audit_aux_data *aux;
  700. ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
  701. if (!ab)
  702. return; /* audit_panic has been called */
  703. audit_log_format(ab, "arch=%x syscall=%d",
  704. context->arch, context->major);
  705. if (context->personality != PER_LINUX)
  706. audit_log_format(ab, " per=%lx", context->personality);
  707. if (context->return_valid)
  708. audit_log_format(ab, " success=%s exit=%ld",
  709. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  710. context->return_code);
  711. audit_log_format(ab,
  712. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  713. " pid=%d auid=%u uid=%u gid=%u"
  714. " euid=%u suid=%u fsuid=%u"
  715. " egid=%u sgid=%u fsgid=%u",
  716. context->argv[0],
  717. context->argv[1],
  718. context->argv[2],
  719. context->argv[3],
  720. context->name_count,
  721. context->pid,
  722. context->loginuid,
  723. context->uid,
  724. context->gid,
  725. context->euid, context->suid, context->fsuid,
  726. context->egid, context->sgid, context->fsgid);
  727. audit_log_task_info(ab);
  728. audit_log_end(ab);
  729. for (aux = context->aux; aux; aux = aux->next) {
  730. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  731. if (!ab)
  732. continue; /* audit_panic has been called */
  733. switch (aux->type) {
  734. case AUDIT_IPC: {
  735. struct audit_aux_data_ipcctl *axi = (void *)aux;
  736. audit_log_format(ab,
  737. " qbytes=%lx iuid=%u igid=%u mode=%x",
  738. axi->qbytes, axi->uid, axi->gid, axi->mode);
  739. break; }
  740. case AUDIT_SOCKETCALL: {
  741. int i;
  742. struct audit_aux_data_socketcall *axs = (void *)aux;
  743. audit_log_format(ab, "nargs=%d", axs->nargs);
  744. for (i=0; i<axs->nargs; i++)
  745. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  746. break; }
  747. case AUDIT_SOCKADDR: {
  748. struct audit_aux_data_sockaddr *axs = (void *)aux;
  749. audit_log_format(ab, "saddr=");
  750. audit_log_hex(ab, axs->a, axs->len);
  751. break; }
  752. case AUDIT_AVC_PATH: {
  753. struct audit_aux_data_path *axi = (void *)aux;
  754. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  755. break; }
  756. }
  757. audit_log_end(ab);
  758. }
  759. if (context->pwd && context->pwdmnt) {
  760. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  761. if (ab) {
  762. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  763. audit_log_end(ab);
  764. }
  765. }
  766. for (i = 0; i < context->name_count; i++) {
  767. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  768. if (!ab)
  769. continue; /* audit_panic has been called */
  770. audit_log_format(ab, "item=%d", i);
  771. if (context->names[i].name) {
  772. audit_log_format(ab, " name=");
  773. audit_log_untrustedstring(ab, context->names[i].name);
  774. }
  775. audit_log_format(ab, " flags=%x\n", context->names[i].flags);
  776. if (context->names[i].ino != (unsigned long)-1)
  777. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
  778. " ouid=%u ogid=%u rdev=%02x:%02x",
  779. context->names[i].ino,
  780. MAJOR(context->names[i].dev),
  781. MINOR(context->names[i].dev),
  782. context->names[i].mode,
  783. context->names[i].uid,
  784. context->names[i].gid,
  785. MAJOR(context->names[i].rdev),
  786. MINOR(context->names[i].rdev));
  787. audit_log_end(ab);
  788. }
  789. }
  790. /* Free a per-task audit context. Called from copy_process and
  791. * __put_task_struct. */
  792. void audit_free(struct task_struct *tsk)
  793. {
  794. struct audit_context *context;
  795. task_lock(tsk);
  796. context = audit_get_context(tsk, 0, 0);
  797. task_unlock(tsk);
  798. if (likely(!context))
  799. return;
  800. /* Check for system calls that do not go through the exit
  801. * function (e.g., exit_group), then free context block.
  802. * We use GFP_ATOMIC here because we might be doing this
  803. * in the context of the idle thread */
  804. if (context->in_syscall && context->auditable)
  805. audit_log_exit(context, GFP_ATOMIC);
  806. audit_free_context(context);
  807. }
  808. /* Fill in audit context at syscall entry. This only happens if the
  809. * audit context was created when the task was created and the state or
  810. * filters demand the audit context be built. If the state from the
  811. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  812. * then the record will be written at syscall exit time (otherwise, it
  813. * will only be written if another part of the kernel requests that it
  814. * be written). */
  815. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  816. unsigned long a1, unsigned long a2,
  817. unsigned long a3, unsigned long a4)
  818. {
  819. struct audit_context *context = tsk->audit_context;
  820. enum audit_state state;
  821. BUG_ON(!context);
  822. /* This happens only on certain architectures that make system
  823. * calls in kernel_thread via the entry.S interface, instead of
  824. * with direct calls. (If you are porting to a new
  825. * architecture, hitting this condition can indicate that you
  826. * got the _exit/_leave calls backward in entry.S.)
  827. *
  828. * i386 no
  829. * x86_64 no
  830. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  831. *
  832. * This also happens with vm86 emulation in a non-nested manner
  833. * (entries without exits), so this case must be caught.
  834. */
  835. if (context->in_syscall) {
  836. struct audit_context *newctx;
  837. #if defined(__NR_vm86) && defined(__NR_vm86old)
  838. /* vm86 mode should only be entered once */
  839. if (major == __NR_vm86 || major == __NR_vm86old)
  840. return;
  841. #endif
  842. #if AUDIT_DEBUG
  843. printk(KERN_ERR
  844. "audit(:%d) pid=%d in syscall=%d;"
  845. " entering syscall=%d\n",
  846. context->serial, tsk->pid, context->major, major);
  847. #endif
  848. newctx = audit_alloc_context(context->state);
  849. if (newctx) {
  850. newctx->previous = context;
  851. context = newctx;
  852. tsk->audit_context = newctx;
  853. } else {
  854. /* If we can't alloc a new context, the best we
  855. * can do is to leak memory (any pending putname
  856. * will be lost). The only other alternative is
  857. * to abandon auditing. */
  858. audit_zero_context(context, context->state);
  859. }
  860. }
  861. BUG_ON(context->in_syscall || context->name_count);
  862. if (!audit_enabled)
  863. return;
  864. context->arch = arch;
  865. context->major = major;
  866. context->argv[0] = a1;
  867. context->argv[1] = a2;
  868. context->argv[2] = a3;
  869. context->argv[3] = a4;
  870. state = context->state;
  871. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  872. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  873. if (likely(state == AUDIT_DISABLED))
  874. return;
  875. context->serial = 0;
  876. context->ctime = CURRENT_TIME;
  877. context->in_syscall = 1;
  878. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  879. }
  880. /* Tear down after system call. If the audit context has been marked as
  881. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  882. * filtering, or because some other part of the kernel write an audit
  883. * message), then write out the syscall information. In call cases,
  884. * free the names stored from getname(). */
  885. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  886. {
  887. struct audit_context *context;
  888. get_task_struct(tsk);
  889. task_lock(tsk);
  890. context = audit_get_context(tsk, valid, return_code);
  891. task_unlock(tsk);
  892. /* Not having a context here is ok, since the parent may have
  893. * called __put_task_struct. */
  894. if (likely(!context))
  895. return;
  896. if (context->in_syscall && context->auditable)
  897. audit_log_exit(context, GFP_KERNEL);
  898. context->in_syscall = 0;
  899. context->auditable = 0;
  900. if (context->previous) {
  901. struct audit_context *new_context = context->previous;
  902. context->previous = NULL;
  903. audit_free_context(context);
  904. tsk->audit_context = new_context;
  905. } else {
  906. audit_free_names(context);
  907. audit_free_aux(context);
  908. audit_zero_context(context, context->state);
  909. tsk->audit_context = context;
  910. }
  911. put_task_struct(tsk);
  912. }
  913. /* Add a name to the list. Called from fs/namei.c:getname(). */
  914. void audit_getname(const char *name)
  915. {
  916. struct audit_context *context = current->audit_context;
  917. if (!context || IS_ERR(name) || !name)
  918. return;
  919. if (!context->in_syscall) {
  920. #if AUDIT_DEBUG == 2
  921. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  922. __FILE__, __LINE__, context->serial, name);
  923. dump_stack();
  924. #endif
  925. return;
  926. }
  927. BUG_ON(context->name_count >= AUDIT_NAMES);
  928. context->names[context->name_count].name = name;
  929. context->names[context->name_count].ino = (unsigned long)-1;
  930. ++context->name_count;
  931. if (!context->pwd) {
  932. read_lock(&current->fs->lock);
  933. context->pwd = dget(current->fs->pwd);
  934. context->pwdmnt = mntget(current->fs->pwdmnt);
  935. read_unlock(&current->fs->lock);
  936. }
  937. }
  938. /* Intercept a putname request. Called from
  939. * include/linux/fs.h:putname(). If we have stored the name from
  940. * getname in the audit context, then we delay the putname until syscall
  941. * exit. */
  942. void audit_putname(const char *name)
  943. {
  944. struct audit_context *context = current->audit_context;
  945. BUG_ON(!context);
  946. if (!context->in_syscall) {
  947. #if AUDIT_DEBUG == 2
  948. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  949. __FILE__, __LINE__, context->serial, name);
  950. if (context->name_count) {
  951. int i;
  952. for (i = 0; i < context->name_count; i++)
  953. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  954. context->names[i].name,
  955. context->names[i].name);
  956. }
  957. #endif
  958. __putname(name);
  959. }
  960. #if AUDIT_DEBUG
  961. else {
  962. ++context->put_count;
  963. if (context->put_count > context->name_count) {
  964. printk(KERN_ERR "%s:%d(:%d): major=%d"
  965. " in_syscall=%d putname(%p) name_count=%d"
  966. " put_count=%d\n",
  967. __FILE__, __LINE__,
  968. context->serial, context->major,
  969. context->in_syscall, name, context->name_count,
  970. context->put_count);
  971. dump_stack();
  972. }
  973. }
  974. #endif
  975. }
  976. /* Store the inode and device from a lookup. Called from
  977. * fs/namei.c:path_lookup(). */
  978. void audit_inode(const char *name, const struct inode *inode, unsigned flags)
  979. {
  980. int idx;
  981. struct audit_context *context = current->audit_context;
  982. if (!context->in_syscall)
  983. return;
  984. if (context->name_count
  985. && context->names[context->name_count-1].name
  986. && context->names[context->name_count-1].name == name)
  987. idx = context->name_count - 1;
  988. else if (context->name_count > 1
  989. && context->names[context->name_count-2].name
  990. && context->names[context->name_count-2].name == name)
  991. idx = context->name_count - 2;
  992. else {
  993. /* FIXME: how much do we care about inodes that have no
  994. * associated name? */
  995. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  996. return;
  997. idx = context->name_count++;
  998. context->names[idx].name = NULL;
  999. #if AUDIT_DEBUG
  1000. ++context->ino_count;
  1001. #endif
  1002. }
  1003. context->names[idx].flags = flags;
  1004. context->names[idx].ino = inode->i_ino;
  1005. context->names[idx].dev = inode->i_sb->s_dev;
  1006. context->names[idx].mode = inode->i_mode;
  1007. context->names[idx].uid = inode->i_uid;
  1008. context->names[idx].gid = inode->i_gid;
  1009. context->names[idx].rdev = inode->i_rdev;
  1010. }
  1011. void auditsc_get_stamp(struct audit_context *ctx,
  1012. struct timespec *t, unsigned int *serial)
  1013. {
  1014. if (!ctx->serial)
  1015. ctx->serial = audit_serial();
  1016. t->tv_sec = ctx->ctime.tv_sec;
  1017. t->tv_nsec = ctx->ctime.tv_nsec;
  1018. *serial = ctx->serial;
  1019. ctx->auditable = 1;
  1020. }
  1021. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1022. {
  1023. if (task->audit_context) {
  1024. struct audit_buffer *ab;
  1025. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1026. if (ab) {
  1027. audit_log_format(ab, "login pid=%d uid=%u "
  1028. "old auid=%u new auid=%u",
  1029. task->pid, task->uid,
  1030. task->audit_context->loginuid, loginuid);
  1031. audit_log_end(ab);
  1032. }
  1033. task->audit_context->loginuid = loginuid;
  1034. }
  1035. return 0;
  1036. }
  1037. uid_t audit_get_loginuid(struct audit_context *ctx)
  1038. {
  1039. return ctx ? ctx->loginuid : -1;
  1040. }
  1041. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1042. {
  1043. struct audit_aux_data_ipcctl *ax;
  1044. struct audit_context *context = current->audit_context;
  1045. if (likely(!context))
  1046. return 0;
  1047. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1048. if (!ax)
  1049. return -ENOMEM;
  1050. ax->qbytes = qbytes;
  1051. ax->uid = uid;
  1052. ax->gid = gid;
  1053. ax->mode = mode;
  1054. ax->d.type = AUDIT_IPC;
  1055. ax->d.next = context->aux;
  1056. context->aux = (void *)ax;
  1057. return 0;
  1058. }
  1059. int audit_socketcall(int nargs, unsigned long *args)
  1060. {
  1061. struct audit_aux_data_socketcall *ax;
  1062. struct audit_context *context = current->audit_context;
  1063. if (likely(!context))
  1064. return 0;
  1065. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1066. if (!ax)
  1067. return -ENOMEM;
  1068. ax->nargs = nargs;
  1069. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1070. ax->d.type = AUDIT_SOCKETCALL;
  1071. ax->d.next = context->aux;
  1072. context->aux = (void *)ax;
  1073. return 0;
  1074. }
  1075. int audit_sockaddr(int len, void *a)
  1076. {
  1077. struct audit_aux_data_sockaddr *ax;
  1078. struct audit_context *context = current->audit_context;
  1079. if (likely(!context))
  1080. return 0;
  1081. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1082. if (!ax)
  1083. return -ENOMEM;
  1084. ax->len = len;
  1085. memcpy(ax->a, a, len);
  1086. ax->d.type = AUDIT_SOCKADDR;
  1087. ax->d.next = context->aux;
  1088. context->aux = (void *)ax;
  1089. return 0;
  1090. }
  1091. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1092. {
  1093. struct audit_aux_data_path *ax;
  1094. struct audit_context *context = current->audit_context;
  1095. if (likely(!context))
  1096. return 0;
  1097. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1098. if (!ax)
  1099. return -ENOMEM;
  1100. ax->dentry = dget(dentry);
  1101. ax->mnt = mntget(mnt);
  1102. ax->d.type = AUDIT_AVC_PATH;
  1103. ax->d.next = context->aux;
  1104. context->aux = (void *)ax;
  1105. return 0;
  1106. }
  1107. void audit_signal_info(int sig, struct task_struct *t)
  1108. {
  1109. extern pid_t audit_sig_pid;
  1110. extern uid_t audit_sig_uid;
  1111. if (unlikely(audit_pid && t->tgid == audit_pid)) {
  1112. if (sig == SIGTERM || sig == SIGHUP) {
  1113. struct audit_context *ctx = current->audit_context;
  1114. audit_sig_pid = current->pid;
  1115. if (ctx)
  1116. audit_sig_uid = ctx->loginuid;
  1117. else
  1118. audit_sig_uid = current->uid;
  1119. }
  1120. }
  1121. }