auditsc.c 34 KB

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