auditsc.c 36 KB

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