auditsc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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, 2006 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. * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
  33. * 2006.
  34. *
  35. * The support of additional filter rules compares (>, <, >=, <=) was
  36. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  37. *
  38. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  39. * filesystem information.
  40. *
  41. * Subject and object context labeling support added by <danjones@us.ibm.com>
  42. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  43. */
  44. #include <linux/init.h>
  45. #include <asm/types.h>
  46. #include <asm/atomic.h>
  47. #include <asm/types.h>
  48. #include <linux/fs.h>
  49. #include <linux/namei.h>
  50. #include <linux/mm.h>
  51. #include <linux/module.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/selinux.h>
  65. #include <linux/binfmts.h>
  66. #include <linux/syscalls.h>
  67. #include "audit.h"
  68. extern struct list_head audit_filter_list[];
  69. /* No syscall auditing will take place unless audit_enabled != 0. */
  70. extern int audit_enabled;
  71. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  72. * for saving names from getname(). */
  73. #define AUDIT_NAMES 20
  74. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  75. * audit_context from being used for nameless inodes from
  76. * path_lookup. */
  77. #define AUDIT_NAMES_RESERVED 7
  78. /* Indicates that audit should log the full pathname. */
  79. #define AUDIT_NAME_FULL -1
  80. /* When fs/namei.c:getname() is called, we store the pointer in name and
  81. * we don't let putname() free it (instead we free all of the saved
  82. * pointers at syscall exit time).
  83. *
  84. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  85. struct audit_names {
  86. const char *name;
  87. int name_len; /* number of name's characters to log */
  88. unsigned name_put; /* call __putname() for this name */
  89. unsigned long ino;
  90. dev_t dev;
  91. umode_t mode;
  92. uid_t uid;
  93. gid_t gid;
  94. dev_t rdev;
  95. u32 osid;
  96. };
  97. struct audit_aux_data {
  98. struct audit_aux_data *next;
  99. int type;
  100. };
  101. #define AUDIT_AUX_IPCPERM 0
  102. struct audit_aux_data_mq_open {
  103. struct audit_aux_data d;
  104. int oflag;
  105. mode_t mode;
  106. struct mq_attr attr;
  107. };
  108. struct audit_aux_data_mq_sendrecv {
  109. struct audit_aux_data d;
  110. mqd_t mqdes;
  111. size_t msg_len;
  112. unsigned int msg_prio;
  113. struct timespec abs_timeout;
  114. };
  115. struct audit_aux_data_mq_notify {
  116. struct audit_aux_data d;
  117. mqd_t mqdes;
  118. struct sigevent notification;
  119. };
  120. struct audit_aux_data_mq_getsetattr {
  121. struct audit_aux_data d;
  122. mqd_t mqdes;
  123. struct mq_attr mqstat;
  124. };
  125. struct audit_aux_data_ipcctl {
  126. struct audit_aux_data d;
  127. struct ipc_perm p;
  128. unsigned long qbytes;
  129. uid_t uid;
  130. gid_t gid;
  131. mode_t mode;
  132. u32 osid;
  133. };
  134. struct audit_aux_data_execve {
  135. struct audit_aux_data d;
  136. int argc;
  137. int envc;
  138. char mem[0];
  139. };
  140. struct audit_aux_data_socketcall {
  141. struct audit_aux_data d;
  142. int nargs;
  143. unsigned long args[0];
  144. };
  145. struct audit_aux_data_sockaddr {
  146. struct audit_aux_data d;
  147. int len;
  148. char a[0];
  149. };
  150. struct audit_aux_data_path {
  151. struct audit_aux_data d;
  152. struct dentry *dentry;
  153. struct vfsmount *mnt;
  154. };
  155. /* The per-task audit context. */
  156. struct audit_context {
  157. int in_syscall; /* 1 if task is in a syscall */
  158. enum audit_state state;
  159. unsigned int serial; /* serial number for record */
  160. struct timespec ctime; /* time of syscall entry */
  161. uid_t loginuid; /* login uid (identity) */
  162. int major; /* syscall number */
  163. unsigned long argv[4]; /* syscall arguments */
  164. int return_valid; /* return code is valid */
  165. long return_code;/* syscall return code */
  166. int auditable; /* 1 if record should be written */
  167. int name_count;
  168. struct audit_names names[AUDIT_NAMES];
  169. char * filterkey; /* key for rule that triggered record */
  170. struct dentry * pwd;
  171. struct vfsmount * pwdmnt;
  172. struct audit_context *previous; /* For nested syscalls */
  173. struct audit_aux_data *aux;
  174. /* Save things to print about task_struct */
  175. pid_t pid, ppid;
  176. uid_t uid, euid, suid, fsuid;
  177. gid_t gid, egid, sgid, fsgid;
  178. unsigned long personality;
  179. int arch;
  180. #if AUDIT_DEBUG
  181. int put_count;
  182. int ino_count;
  183. #endif
  184. };
  185. /* Determine if any context name data matches a rule's watch data */
  186. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  187. * otherwise. */
  188. static int audit_filter_rules(struct task_struct *tsk,
  189. struct audit_krule *rule,
  190. struct audit_context *ctx,
  191. struct audit_names *name,
  192. enum audit_state *state)
  193. {
  194. int i, j, need_sid = 1;
  195. u32 sid;
  196. for (i = 0; i < rule->field_count; i++) {
  197. struct audit_field *f = &rule->fields[i];
  198. int result = 0;
  199. switch (f->type) {
  200. case AUDIT_PID:
  201. result = audit_comparator(tsk->pid, f->op, f->val);
  202. break;
  203. case AUDIT_PPID:
  204. if (ctx)
  205. result = audit_comparator(ctx->ppid, f->op, f->val);
  206. break;
  207. case AUDIT_UID:
  208. result = audit_comparator(tsk->uid, f->op, f->val);
  209. break;
  210. case AUDIT_EUID:
  211. result = audit_comparator(tsk->euid, f->op, f->val);
  212. break;
  213. case AUDIT_SUID:
  214. result = audit_comparator(tsk->suid, f->op, f->val);
  215. break;
  216. case AUDIT_FSUID:
  217. result = audit_comparator(tsk->fsuid, f->op, f->val);
  218. break;
  219. case AUDIT_GID:
  220. result = audit_comparator(tsk->gid, f->op, f->val);
  221. break;
  222. case AUDIT_EGID:
  223. result = audit_comparator(tsk->egid, f->op, f->val);
  224. break;
  225. case AUDIT_SGID:
  226. result = audit_comparator(tsk->sgid, f->op, f->val);
  227. break;
  228. case AUDIT_FSGID:
  229. result = audit_comparator(tsk->fsgid, f->op, f->val);
  230. break;
  231. case AUDIT_PERS:
  232. result = audit_comparator(tsk->personality, f->op, f->val);
  233. break;
  234. case AUDIT_ARCH:
  235. if (ctx)
  236. result = audit_comparator(ctx->arch, f->op, f->val);
  237. break;
  238. case AUDIT_EXIT:
  239. if (ctx && ctx->return_valid)
  240. result = audit_comparator(ctx->return_code, f->op, f->val);
  241. break;
  242. case AUDIT_SUCCESS:
  243. if (ctx && ctx->return_valid) {
  244. if (f->val)
  245. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  246. else
  247. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  248. }
  249. break;
  250. case AUDIT_DEVMAJOR:
  251. if (name)
  252. result = audit_comparator(MAJOR(name->dev),
  253. f->op, f->val);
  254. else if (ctx) {
  255. for (j = 0; j < ctx->name_count; j++) {
  256. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  257. ++result;
  258. break;
  259. }
  260. }
  261. }
  262. break;
  263. case AUDIT_DEVMINOR:
  264. if (name)
  265. result = audit_comparator(MINOR(name->dev),
  266. f->op, f->val);
  267. else if (ctx) {
  268. for (j = 0; j < ctx->name_count; j++) {
  269. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  270. ++result;
  271. break;
  272. }
  273. }
  274. }
  275. break;
  276. case AUDIT_INODE:
  277. if (name)
  278. result = (name->ino == f->val);
  279. else if (ctx) {
  280. for (j = 0; j < ctx->name_count; j++) {
  281. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  282. ++result;
  283. break;
  284. }
  285. }
  286. }
  287. break;
  288. case AUDIT_WATCH:
  289. if (name && rule->watch->ino != (unsigned long)-1)
  290. result = (name->dev == rule->watch->dev &&
  291. name->ino == rule->watch->ino);
  292. break;
  293. case AUDIT_LOGINUID:
  294. result = 0;
  295. if (ctx)
  296. result = audit_comparator(ctx->loginuid, f->op, f->val);
  297. break;
  298. case AUDIT_SE_USER:
  299. case AUDIT_SE_ROLE:
  300. case AUDIT_SE_TYPE:
  301. case AUDIT_SE_SEN:
  302. case AUDIT_SE_CLR:
  303. /* NOTE: this may return negative values indicating
  304. a temporary error. We simply treat this as a
  305. match for now to avoid losing information that
  306. may be wanted. An error message will also be
  307. logged upon error */
  308. if (f->se_rule) {
  309. if (need_sid) {
  310. selinux_task_ctxid(tsk, &sid);
  311. need_sid = 0;
  312. }
  313. result = selinux_audit_rule_match(sid, f->type,
  314. f->op,
  315. f->se_rule,
  316. ctx);
  317. }
  318. break;
  319. case AUDIT_ARG0:
  320. case AUDIT_ARG1:
  321. case AUDIT_ARG2:
  322. case AUDIT_ARG3:
  323. if (ctx)
  324. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  325. break;
  326. case AUDIT_FILTERKEY:
  327. /* ignore this field for filtering */
  328. result = 1;
  329. break;
  330. }
  331. if (!result)
  332. return 0;
  333. }
  334. if (rule->filterkey)
  335. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  336. switch (rule->action) {
  337. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  338. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  339. }
  340. return 1;
  341. }
  342. /* At process creation time, we can determine if system-call auditing is
  343. * completely disabled for this task. Since we only have the task
  344. * structure at this point, we can only check uid and gid.
  345. */
  346. static enum audit_state audit_filter_task(struct task_struct *tsk)
  347. {
  348. struct audit_entry *e;
  349. enum audit_state state;
  350. rcu_read_lock();
  351. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  352. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  353. rcu_read_unlock();
  354. return state;
  355. }
  356. }
  357. rcu_read_unlock();
  358. return AUDIT_BUILD_CONTEXT;
  359. }
  360. /* At syscall entry and exit time, this filter is called if the
  361. * audit_state is not low enough that auditing cannot take place, but is
  362. * also not high enough that we already know we have to write an audit
  363. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  364. */
  365. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  366. struct audit_context *ctx,
  367. struct list_head *list)
  368. {
  369. struct audit_entry *e;
  370. enum audit_state state;
  371. if (audit_pid && tsk->tgid == audit_pid)
  372. return AUDIT_DISABLED;
  373. rcu_read_lock();
  374. if (!list_empty(list)) {
  375. int word = AUDIT_WORD(ctx->major);
  376. int bit = AUDIT_BIT(ctx->major);
  377. list_for_each_entry_rcu(e, list, list) {
  378. if ((e->rule.mask[word] & bit) == bit &&
  379. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  380. &state)) {
  381. rcu_read_unlock();
  382. return state;
  383. }
  384. }
  385. }
  386. rcu_read_unlock();
  387. return AUDIT_BUILD_CONTEXT;
  388. }
  389. /* At syscall exit time, this filter is called if any audit_names[] have been
  390. * collected during syscall processing. We only check rules in sublists at hash
  391. * buckets applicable to the inode numbers in audit_names[].
  392. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  393. */
  394. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  395. struct audit_context *ctx)
  396. {
  397. int i;
  398. struct audit_entry *e;
  399. enum audit_state state;
  400. if (audit_pid && tsk->tgid == audit_pid)
  401. return AUDIT_DISABLED;
  402. rcu_read_lock();
  403. for (i = 0; i < ctx->name_count; i++) {
  404. int word = AUDIT_WORD(ctx->major);
  405. int bit = AUDIT_BIT(ctx->major);
  406. struct audit_names *n = &ctx->names[i];
  407. int h = audit_hash_ino((u32)n->ino);
  408. struct list_head *list = &audit_inode_hash[h];
  409. if (list_empty(list))
  410. continue;
  411. list_for_each_entry_rcu(e, list, list) {
  412. if ((e->rule.mask[word] & bit) == bit &&
  413. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  414. rcu_read_unlock();
  415. return state;
  416. }
  417. }
  418. }
  419. rcu_read_unlock();
  420. return AUDIT_BUILD_CONTEXT;
  421. }
  422. void audit_set_auditable(struct audit_context *ctx)
  423. {
  424. ctx->auditable = 1;
  425. }
  426. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  427. int return_valid,
  428. int return_code)
  429. {
  430. struct audit_context *context = tsk->audit_context;
  431. if (likely(!context))
  432. return NULL;
  433. context->return_valid = return_valid;
  434. context->return_code = return_code;
  435. if (context->in_syscall && !context->auditable) {
  436. enum audit_state state;
  437. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  438. if (state == AUDIT_RECORD_CONTEXT) {
  439. context->auditable = 1;
  440. goto get_context;
  441. }
  442. state = audit_filter_inodes(tsk, context);
  443. if (state == AUDIT_RECORD_CONTEXT)
  444. context->auditable = 1;
  445. }
  446. get_context:
  447. context->pid = tsk->pid;
  448. context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
  449. context->uid = tsk->uid;
  450. context->gid = tsk->gid;
  451. context->euid = tsk->euid;
  452. context->suid = tsk->suid;
  453. context->fsuid = tsk->fsuid;
  454. context->egid = tsk->egid;
  455. context->sgid = tsk->sgid;
  456. context->fsgid = tsk->fsgid;
  457. context->personality = tsk->personality;
  458. tsk->audit_context = NULL;
  459. return context;
  460. }
  461. static inline void audit_free_names(struct audit_context *context)
  462. {
  463. int i;
  464. #if AUDIT_DEBUG == 2
  465. if (context->auditable
  466. ||context->put_count + context->ino_count != context->name_count) {
  467. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  468. " name_count=%d put_count=%d"
  469. " ino_count=%d [NOT freeing]\n",
  470. __FILE__, __LINE__,
  471. context->serial, context->major, context->in_syscall,
  472. context->name_count, context->put_count,
  473. context->ino_count);
  474. for (i = 0; i < context->name_count; i++) {
  475. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  476. context->names[i].name,
  477. context->names[i].name ?: "(null)");
  478. }
  479. dump_stack();
  480. return;
  481. }
  482. #endif
  483. #if AUDIT_DEBUG
  484. context->put_count = 0;
  485. context->ino_count = 0;
  486. #endif
  487. for (i = 0; i < context->name_count; i++) {
  488. if (context->names[i].name && context->names[i].name_put)
  489. __putname(context->names[i].name);
  490. }
  491. context->name_count = 0;
  492. if (context->pwd)
  493. dput(context->pwd);
  494. if (context->pwdmnt)
  495. mntput(context->pwdmnt);
  496. context->pwd = NULL;
  497. context->pwdmnt = NULL;
  498. }
  499. static inline void audit_free_aux(struct audit_context *context)
  500. {
  501. struct audit_aux_data *aux;
  502. while ((aux = context->aux)) {
  503. if (aux->type == AUDIT_AVC_PATH) {
  504. struct audit_aux_data_path *axi = (void *)aux;
  505. dput(axi->dentry);
  506. mntput(axi->mnt);
  507. }
  508. context->aux = aux->next;
  509. kfree(aux);
  510. }
  511. }
  512. static inline void audit_zero_context(struct audit_context *context,
  513. enum audit_state state)
  514. {
  515. uid_t loginuid = context->loginuid;
  516. memset(context, 0, sizeof(*context));
  517. context->state = state;
  518. context->loginuid = loginuid;
  519. }
  520. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  521. {
  522. struct audit_context *context;
  523. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  524. return NULL;
  525. audit_zero_context(context, state);
  526. return context;
  527. }
  528. /**
  529. * audit_alloc - allocate an audit context block for a task
  530. * @tsk: task
  531. *
  532. * Filter on the task information and allocate a per-task audit context
  533. * if necessary. Doing so turns on system call auditing for the
  534. * specified task. This is called from copy_process, so no lock is
  535. * needed.
  536. */
  537. int audit_alloc(struct task_struct *tsk)
  538. {
  539. struct audit_context *context;
  540. enum audit_state state;
  541. if (likely(!audit_enabled))
  542. return 0; /* Return if not auditing. */
  543. state = audit_filter_task(tsk);
  544. if (likely(state == AUDIT_DISABLED))
  545. return 0;
  546. if (!(context = audit_alloc_context(state))) {
  547. audit_log_lost("out of memory in audit_alloc");
  548. return -ENOMEM;
  549. }
  550. /* Preserve login uid */
  551. context->loginuid = -1;
  552. if (current->audit_context)
  553. context->loginuid = current->audit_context->loginuid;
  554. tsk->audit_context = context;
  555. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  556. return 0;
  557. }
  558. static inline void audit_free_context(struct audit_context *context)
  559. {
  560. struct audit_context *previous;
  561. int count = 0;
  562. do {
  563. previous = context->previous;
  564. if (previous || (count && count < 10)) {
  565. ++count;
  566. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  567. " freeing multiple contexts (%d)\n",
  568. context->serial, context->major,
  569. context->name_count, count);
  570. }
  571. audit_free_names(context);
  572. audit_free_aux(context);
  573. kfree(context->filterkey);
  574. kfree(context);
  575. context = previous;
  576. } while (context);
  577. if (count >= 10)
  578. printk(KERN_ERR "audit: freed %d contexts\n", count);
  579. }
  580. static void audit_log_task_context(struct audit_buffer *ab)
  581. {
  582. char *ctx = NULL;
  583. ssize_t len = 0;
  584. len = security_getprocattr(current, "current", NULL, 0);
  585. if (len < 0) {
  586. if (len != -EINVAL)
  587. goto error_path;
  588. return;
  589. }
  590. ctx = kmalloc(len, GFP_KERNEL);
  591. if (!ctx)
  592. goto error_path;
  593. len = security_getprocattr(current, "current", ctx, len);
  594. if (len < 0 )
  595. goto error_path;
  596. audit_log_format(ab, " subj=%s", ctx);
  597. return;
  598. error_path:
  599. kfree(ctx);
  600. audit_panic("error in audit_log_task_context");
  601. return;
  602. }
  603. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  604. {
  605. char name[sizeof(tsk->comm)];
  606. struct mm_struct *mm = tsk->mm;
  607. struct vm_area_struct *vma;
  608. /* tsk == current */
  609. get_task_comm(name, tsk);
  610. audit_log_format(ab, " comm=");
  611. audit_log_untrustedstring(ab, name);
  612. if (mm) {
  613. down_read(&mm->mmap_sem);
  614. vma = mm->mmap;
  615. while (vma) {
  616. if ((vma->vm_flags & VM_EXECUTABLE) &&
  617. vma->vm_file) {
  618. audit_log_d_path(ab, "exe=",
  619. vma->vm_file->f_dentry,
  620. vma->vm_file->f_vfsmnt);
  621. break;
  622. }
  623. vma = vma->vm_next;
  624. }
  625. up_read(&mm->mmap_sem);
  626. }
  627. audit_log_task_context(ab);
  628. }
  629. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  630. {
  631. int i, call_panic = 0;
  632. struct audit_buffer *ab;
  633. struct audit_aux_data *aux;
  634. const char *tty;
  635. /* tsk == current */
  636. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  637. if (!ab)
  638. return; /* audit_panic has been called */
  639. audit_log_format(ab, "arch=%x syscall=%d",
  640. context->arch, context->major);
  641. if (context->personality != PER_LINUX)
  642. audit_log_format(ab, " per=%lx", context->personality);
  643. if (context->return_valid)
  644. audit_log_format(ab, " success=%s exit=%ld",
  645. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  646. context->return_code);
  647. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  648. tty = tsk->signal->tty->name;
  649. else
  650. tty = "(none)";
  651. audit_log_format(ab,
  652. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  653. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  654. " euid=%u suid=%u fsuid=%u"
  655. " egid=%u sgid=%u fsgid=%u tty=%s",
  656. context->argv[0],
  657. context->argv[1],
  658. context->argv[2],
  659. context->argv[3],
  660. context->name_count,
  661. context->ppid,
  662. context->pid,
  663. context->loginuid,
  664. context->uid,
  665. context->gid,
  666. context->euid, context->suid, context->fsuid,
  667. context->egid, context->sgid, context->fsgid, tty);
  668. audit_log_task_info(ab, tsk);
  669. if (context->filterkey) {
  670. audit_log_format(ab, " key=");
  671. audit_log_untrustedstring(ab, context->filterkey);
  672. } else
  673. audit_log_format(ab, " key=(null)");
  674. audit_log_end(ab);
  675. for (aux = context->aux; aux; aux = aux->next) {
  676. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  677. if (!ab)
  678. continue; /* audit_panic has been called */
  679. switch (aux->type) {
  680. case AUDIT_MQ_OPEN: {
  681. struct audit_aux_data_mq_open *axi = (void *)aux;
  682. audit_log_format(ab,
  683. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  684. "mq_msgsize=%ld mq_curmsgs=%ld",
  685. axi->oflag, axi->mode, axi->attr.mq_flags,
  686. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  687. axi->attr.mq_curmsgs);
  688. break; }
  689. case AUDIT_MQ_SENDRECV: {
  690. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  691. audit_log_format(ab,
  692. "mqdes=%d msg_len=%zd msg_prio=%u "
  693. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  694. axi->mqdes, axi->msg_len, axi->msg_prio,
  695. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  696. break; }
  697. case AUDIT_MQ_NOTIFY: {
  698. struct audit_aux_data_mq_notify *axi = (void *)aux;
  699. audit_log_format(ab,
  700. "mqdes=%d sigev_signo=%d",
  701. axi->mqdes,
  702. axi->notification.sigev_signo);
  703. break; }
  704. case AUDIT_MQ_GETSETATTR: {
  705. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  706. audit_log_format(ab,
  707. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  708. "mq_curmsgs=%ld ",
  709. axi->mqdes,
  710. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  711. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  712. break; }
  713. case AUDIT_IPC: {
  714. struct audit_aux_data_ipcctl *axi = (void *)aux;
  715. audit_log_format(ab,
  716. "ouid=%u ogid=%u mode=%x",
  717. axi->uid, axi->gid, axi->mode);
  718. if (axi->osid != 0) {
  719. char *ctx = NULL;
  720. u32 len;
  721. if (selinux_ctxid_to_string(
  722. axi->osid, &ctx, &len)) {
  723. audit_log_format(ab, " osid=%u",
  724. axi->osid);
  725. call_panic = 1;
  726. } else
  727. audit_log_format(ab, " obj=%s", ctx);
  728. kfree(ctx);
  729. }
  730. break; }
  731. case AUDIT_IPC_SET_PERM: {
  732. struct audit_aux_data_ipcctl *axi = (void *)aux;
  733. audit_log_format(ab,
  734. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  735. axi->qbytes, axi->uid, axi->gid, axi->mode);
  736. break; }
  737. case AUDIT_EXECVE: {
  738. struct audit_aux_data_execve *axi = (void *)aux;
  739. int i;
  740. const char *p;
  741. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  742. audit_log_format(ab, "a%d=", i);
  743. p = audit_log_untrustedstring(ab, p);
  744. audit_log_format(ab, "\n");
  745. }
  746. break; }
  747. case AUDIT_SOCKETCALL: {
  748. int i;
  749. struct audit_aux_data_socketcall *axs = (void *)aux;
  750. audit_log_format(ab, "nargs=%d", axs->nargs);
  751. for (i=0; i<axs->nargs; i++)
  752. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  753. break; }
  754. case AUDIT_SOCKADDR: {
  755. struct audit_aux_data_sockaddr *axs = (void *)aux;
  756. audit_log_format(ab, "saddr=");
  757. audit_log_hex(ab, axs->a, axs->len);
  758. break; }
  759. case AUDIT_AVC_PATH: {
  760. struct audit_aux_data_path *axi = (void *)aux;
  761. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  762. break; }
  763. }
  764. audit_log_end(ab);
  765. }
  766. if (context->pwd && context->pwdmnt) {
  767. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  768. if (ab) {
  769. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  770. audit_log_end(ab);
  771. }
  772. }
  773. for (i = 0; i < context->name_count; i++) {
  774. struct audit_names *n = &context->names[i];
  775. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  776. if (!ab)
  777. continue; /* audit_panic has been called */
  778. audit_log_format(ab, "item=%d", i);
  779. if (n->name) {
  780. switch(n->name_len) {
  781. case AUDIT_NAME_FULL:
  782. /* log the full path */
  783. audit_log_format(ab, " name=");
  784. audit_log_untrustedstring(ab, n->name);
  785. break;
  786. case 0:
  787. /* name was specified as a relative path and the
  788. * directory component is the cwd */
  789. audit_log_d_path(ab, " name=", context->pwd,
  790. context->pwdmnt);
  791. break;
  792. default:
  793. /* log the name's directory component */
  794. audit_log_format(ab, " name=");
  795. audit_log_n_untrustedstring(ab, n->name_len,
  796. n->name);
  797. }
  798. } else
  799. audit_log_format(ab, " name=(null)");
  800. if (n->ino != (unsigned long)-1) {
  801. audit_log_format(ab, " inode=%lu"
  802. " dev=%02x:%02x mode=%#o"
  803. " ouid=%u ogid=%u rdev=%02x:%02x",
  804. n->ino,
  805. MAJOR(n->dev),
  806. MINOR(n->dev),
  807. n->mode,
  808. n->uid,
  809. n->gid,
  810. MAJOR(n->rdev),
  811. MINOR(n->rdev));
  812. }
  813. if (n->osid != 0) {
  814. char *ctx = NULL;
  815. u32 len;
  816. if (selinux_ctxid_to_string(
  817. n->osid, &ctx, &len)) {
  818. audit_log_format(ab, " osid=%u", n->osid);
  819. call_panic = 2;
  820. } else
  821. audit_log_format(ab, " obj=%s", ctx);
  822. kfree(ctx);
  823. }
  824. audit_log_end(ab);
  825. }
  826. if (call_panic)
  827. audit_panic("error converting sid to string");
  828. }
  829. /**
  830. * audit_free - free a per-task audit context
  831. * @tsk: task whose audit context block to free
  832. *
  833. * Called from copy_process and do_exit
  834. */
  835. void audit_free(struct task_struct *tsk)
  836. {
  837. struct audit_context *context;
  838. context = audit_get_context(tsk, 0, 0);
  839. if (likely(!context))
  840. return;
  841. /* Check for system calls that do not go through the exit
  842. * function (e.g., exit_group), then free context block.
  843. * We use GFP_ATOMIC here because we might be doing this
  844. * in the context of the idle thread */
  845. /* that can happen only if we are called from do_exit() */
  846. if (context->in_syscall && context->auditable)
  847. audit_log_exit(context, tsk);
  848. audit_free_context(context);
  849. }
  850. /**
  851. * audit_syscall_entry - fill in an audit record at syscall entry
  852. * @tsk: task being audited
  853. * @arch: architecture type
  854. * @major: major syscall type (function)
  855. * @a1: additional syscall register 1
  856. * @a2: additional syscall register 2
  857. * @a3: additional syscall register 3
  858. * @a4: additional syscall register 4
  859. *
  860. * Fill in audit context at syscall entry. This only happens if the
  861. * audit context was created when the task was created and the state or
  862. * filters demand the audit context be built. If the state from the
  863. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  864. * then the record will be written at syscall exit time (otherwise, it
  865. * will only be written if another part of the kernel requests that it
  866. * be written).
  867. */
  868. void audit_syscall_entry(int arch, int major,
  869. unsigned long a1, unsigned long a2,
  870. unsigned long a3, unsigned long a4)
  871. {
  872. struct task_struct *tsk = current;
  873. struct audit_context *context = tsk->audit_context;
  874. enum audit_state state;
  875. BUG_ON(!context);
  876. /*
  877. * This happens only on certain architectures that make system
  878. * calls in kernel_thread via the entry.S interface, instead of
  879. * with direct calls. (If you are porting to a new
  880. * architecture, hitting this condition can indicate that you
  881. * got the _exit/_leave calls backward in entry.S.)
  882. *
  883. * i386 no
  884. * x86_64 no
  885. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  886. *
  887. * This also happens with vm86 emulation in a non-nested manner
  888. * (entries without exits), so this case must be caught.
  889. */
  890. if (context->in_syscall) {
  891. struct audit_context *newctx;
  892. #if AUDIT_DEBUG
  893. printk(KERN_ERR
  894. "audit(:%d) pid=%d in syscall=%d;"
  895. " entering syscall=%d\n",
  896. context->serial, tsk->pid, context->major, major);
  897. #endif
  898. newctx = audit_alloc_context(context->state);
  899. if (newctx) {
  900. newctx->previous = context;
  901. context = newctx;
  902. tsk->audit_context = newctx;
  903. } else {
  904. /* If we can't alloc a new context, the best we
  905. * can do is to leak memory (any pending putname
  906. * will be lost). The only other alternative is
  907. * to abandon auditing. */
  908. audit_zero_context(context, context->state);
  909. }
  910. }
  911. BUG_ON(context->in_syscall || context->name_count);
  912. if (!audit_enabled)
  913. return;
  914. context->arch = arch;
  915. context->major = major;
  916. context->argv[0] = a1;
  917. context->argv[1] = a2;
  918. context->argv[2] = a3;
  919. context->argv[3] = a4;
  920. state = context->state;
  921. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  922. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  923. if (likely(state == AUDIT_DISABLED))
  924. return;
  925. context->serial = 0;
  926. context->ctime = CURRENT_TIME;
  927. context->in_syscall = 1;
  928. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  929. }
  930. /**
  931. * audit_syscall_exit - deallocate audit context after a system call
  932. * @tsk: task being audited
  933. * @valid: success/failure flag
  934. * @return_code: syscall return value
  935. *
  936. * Tear down after system call. If the audit context has been marked as
  937. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  938. * filtering, or because some other part of the kernel write an audit
  939. * message), then write out the syscall information. In call cases,
  940. * free the names stored from getname().
  941. */
  942. void audit_syscall_exit(int valid, long return_code)
  943. {
  944. struct task_struct *tsk = current;
  945. struct audit_context *context;
  946. context = audit_get_context(tsk, valid, return_code);
  947. if (likely(!context))
  948. return;
  949. if (context->in_syscall && context->auditable)
  950. audit_log_exit(context, tsk);
  951. context->in_syscall = 0;
  952. context->auditable = 0;
  953. if (context->previous) {
  954. struct audit_context *new_context = context->previous;
  955. context->previous = NULL;
  956. audit_free_context(context);
  957. tsk->audit_context = new_context;
  958. } else {
  959. audit_free_names(context);
  960. audit_free_aux(context);
  961. kfree(context->filterkey);
  962. context->filterkey = NULL;
  963. tsk->audit_context = context;
  964. }
  965. }
  966. /**
  967. * audit_getname - add a name to the list
  968. * @name: name to add
  969. *
  970. * Add a name to the list of audit names for this context.
  971. * Called from fs/namei.c:getname().
  972. */
  973. void __audit_getname(const char *name)
  974. {
  975. struct audit_context *context = current->audit_context;
  976. if (IS_ERR(name) || !name)
  977. return;
  978. if (!context->in_syscall) {
  979. #if AUDIT_DEBUG == 2
  980. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  981. __FILE__, __LINE__, context->serial, name);
  982. dump_stack();
  983. #endif
  984. return;
  985. }
  986. BUG_ON(context->name_count >= AUDIT_NAMES);
  987. context->names[context->name_count].name = name;
  988. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  989. context->names[context->name_count].name_put = 1;
  990. context->names[context->name_count].ino = (unsigned long)-1;
  991. ++context->name_count;
  992. if (!context->pwd) {
  993. read_lock(&current->fs->lock);
  994. context->pwd = dget(current->fs->pwd);
  995. context->pwdmnt = mntget(current->fs->pwdmnt);
  996. read_unlock(&current->fs->lock);
  997. }
  998. }
  999. /* audit_putname - intercept a putname request
  1000. * @name: name to intercept and delay for putname
  1001. *
  1002. * If we have stored the name from getname in the audit context,
  1003. * then we delay the putname until syscall exit.
  1004. * Called from include/linux/fs.h:putname().
  1005. */
  1006. void audit_putname(const char *name)
  1007. {
  1008. struct audit_context *context = current->audit_context;
  1009. BUG_ON(!context);
  1010. if (!context->in_syscall) {
  1011. #if AUDIT_DEBUG == 2
  1012. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1013. __FILE__, __LINE__, context->serial, name);
  1014. if (context->name_count) {
  1015. int i;
  1016. for (i = 0; i < context->name_count; i++)
  1017. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1018. context->names[i].name,
  1019. context->names[i].name ?: "(null)");
  1020. }
  1021. #endif
  1022. __putname(name);
  1023. }
  1024. #if AUDIT_DEBUG
  1025. else {
  1026. ++context->put_count;
  1027. if (context->put_count > context->name_count) {
  1028. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1029. " in_syscall=%d putname(%p) name_count=%d"
  1030. " put_count=%d\n",
  1031. __FILE__, __LINE__,
  1032. context->serial, context->major,
  1033. context->in_syscall, name, context->name_count,
  1034. context->put_count);
  1035. dump_stack();
  1036. }
  1037. }
  1038. #endif
  1039. }
  1040. static void audit_inode_context(int idx, const struct inode *inode)
  1041. {
  1042. struct audit_context *context = current->audit_context;
  1043. selinux_get_inode_sid(inode, &context->names[idx].osid);
  1044. }
  1045. /**
  1046. * audit_inode - store the inode and device from a lookup
  1047. * @name: name being audited
  1048. * @inode: inode being audited
  1049. *
  1050. * Called from fs/namei.c:path_lookup().
  1051. */
  1052. void __audit_inode(const char *name, const struct inode *inode)
  1053. {
  1054. int idx;
  1055. struct audit_context *context = current->audit_context;
  1056. if (!context->in_syscall)
  1057. return;
  1058. if (context->name_count
  1059. && context->names[context->name_count-1].name
  1060. && context->names[context->name_count-1].name == name)
  1061. idx = context->name_count - 1;
  1062. else if (context->name_count > 1
  1063. && context->names[context->name_count-2].name
  1064. && context->names[context->name_count-2].name == name)
  1065. idx = context->name_count - 2;
  1066. else {
  1067. /* FIXME: how much do we care about inodes that have no
  1068. * associated name? */
  1069. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1070. return;
  1071. idx = context->name_count++;
  1072. context->names[idx].name = NULL;
  1073. #if AUDIT_DEBUG
  1074. ++context->ino_count;
  1075. #endif
  1076. }
  1077. context->names[idx].ino = inode->i_ino;
  1078. context->names[idx].dev = inode->i_sb->s_dev;
  1079. context->names[idx].mode = inode->i_mode;
  1080. context->names[idx].uid = inode->i_uid;
  1081. context->names[idx].gid = inode->i_gid;
  1082. context->names[idx].rdev = inode->i_rdev;
  1083. audit_inode_context(idx, inode);
  1084. }
  1085. /**
  1086. * audit_inode_child - collect inode info for created/removed objects
  1087. * @dname: inode's dentry name
  1088. * @inode: inode being audited
  1089. * @pino: inode number of dentry parent
  1090. *
  1091. * For syscalls that create or remove filesystem objects, audit_inode
  1092. * can only collect information for the filesystem object's parent.
  1093. * This call updates the audit context with the child's information.
  1094. * Syscalls that create a new filesystem object must be hooked after
  1095. * the object is created. Syscalls that remove a filesystem object
  1096. * must be hooked prior, in order to capture the target inode during
  1097. * unsuccessful attempts.
  1098. */
  1099. void __audit_inode_child(const char *dname, const struct inode *inode,
  1100. unsigned long pino)
  1101. {
  1102. int idx;
  1103. struct audit_context *context = current->audit_context;
  1104. const char *found_name = NULL;
  1105. int dirlen = 0;
  1106. if (!context->in_syscall)
  1107. return;
  1108. /* determine matching parent */
  1109. if (!dname)
  1110. goto update_context;
  1111. for (idx = 0; idx < context->name_count; idx++)
  1112. if (context->names[idx].ino == pino) {
  1113. const char *name = context->names[idx].name;
  1114. if (!name)
  1115. continue;
  1116. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1117. context->names[idx].name_len = dirlen;
  1118. found_name = name;
  1119. break;
  1120. }
  1121. }
  1122. update_context:
  1123. idx = context->name_count++;
  1124. #if AUDIT_DEBUG
  1125. context->ino_count++;
  1126. #endif
  1127. /* Re-use the name belonging to the slot for a matching parent directory.
  1128. * All names for this context are relinquished in audit_free_names() */
  1129. context->names[idx].name = found_name;
  1130. context->names[idx].name_len = AUDIT_NAME_FULL;
  1131. context->names[idx].name_put = 0; /* don't call __putname() */
  1132. if (inode) {
  1133. context->names[idx].ino = inode->i_ino;
  1134. context->names[idx].dev = inode->i_sb->s_dev;
  1135. context->names[idx].mode = inode->i_mode;
  1136. context->names[idx].uid = inode->i_uid;
  1137. context->names[idx].gid = inode->i_gid;
  1138. context->names[idx].rdev = inode->i_rdev;
  1139. audit_inode_context(idx, inode);
  1140. } else
  1141. context->names[idx].ino = (unsigned long)-1;
  1142. }
  1143. /**
  1144. * auditsc_get_stamp - get local copies of audit_context values
  1145. * @ctx: audit_context for the task
  1146. * @t: timespec to store time recorded in the audit_context
  1147. * @serial: serial value that is recorded in the audit_context
  1148. *
  1149. * Also sets the context as auditable.
  1150. */
  1151. void auditsc_get_stamp(struct audit_context *ctx,
  1152. struct timespec *t, unsigned int *serial)
  1153. {
  1154. if (!ctx->serial)
  1155. ctx->serial = audit_serial();
  1156. t->tv_sec = ctx->ctime.tv_sec;
  1157. t->tv_nsec = ctx->ctime.tv_nsec;
  1158. *serial = ctx->serial;
  1159. ctx->auditable = 1;
  1160. }
  1161. /**
  1162. * audit_set_loginuid - set a task's audit_context loginuid
  1163. * @task: task whose audit context is being modified
  1164. * @loginuid: loginuid value
  1165. *
  1166. * Returns 0.
  1167. *
  1168. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1169. */
  1170. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1171. {
  1172. struct audit_context *context = task->audit_context;
  1173. if (context) {
  1174. /* Only log if audit is enabled */
  1175. if (context->in_syscall) {
  1176. struct audit_buffer *ab;
  1177. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1178. if (ab) {
  1179. audit_log_format(ab, "login pid=%d uid=%u "
  1180. "old auid=%u new auid=%u",
  1181. task->pid, task->uid,
  1182. context->loginuid, loginuid);
  1183. audit_log_end(ab);
  1184. }
  1185. }
  1186. context->loginuid = loginuid;
  1187. }
  1188. return 0;
  1189. }
  1190. /**
  1191. * audit_get_loginuid - get the loginuid for an audit_context
  1192. * @ctx: the audit_context
  1193. *
  1194. * Returns the context's loginuid or -1 if @ctx is NULL.
  1195. */
  1196. uid_t audit_get_loginuid(struct audit_context *ctx)
  1197. {
  1198. return ctx ? ctx->loginuid : -1;
  1199. }
  1200. /**
  1201. * __audit_mq_open - record audit data for a POSIX MQ open
  1202. * @oflag: open flag
  1203. * @mode: mode bits
  1204. * @u_attr: queue attributes
  1205. *
  1206. * Returns 0 for success or NULL context or < 0 on error.
  1207. */
  1208. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1209. {
  1210. struct audit_aux_data_mq_open *ax;
  1211. struct audit_context *context = current->audit_context;
  1212. if (!audit_enabled)
  1213. return 0;
  1214. if (likely(!context))
  1215. return 0;
  1216. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1217. if (!ax)
  1218. return -ENOMEM;
  1219. if (u_attr != NULL) {
  1220. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1221. kfree(ax);
  1222. return -EFAULT;
  1223. }
  1224. } else
  1225. memset(&ax->attr, 0, sizeof(ax->attr));
  1226. ax->oflag = oflag;
  1227. ax->mode = mode;
  1228. ax->d.type = AUDIT_MQ_OPEN;
  1229. ax->d.next = context->aux;
  1230. context->aux = (void *)ax;
  1231. return 0;
  1232. }
  1233. /**
  1234. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1235. * @mqdes: MQ descriptor
  1236. * @msg_len: Message length
  1237. * @msg_prio: Message priority
  1238. * @u_abs_timeout: Message timeout in absolute time
  1239. *
  1240. * Returns 0 for success or NULL context or < 0 on error.
  1241. */
  1242. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1243. const struct timespec __user *u_abs_timeout)
  1244. {
  1245. struct audit_aux_data_mq_sendrecv *ax;
  1246. struct audit_context *context = current->audit_context;
  1247. if (!audit_enabled)
  1248. return 0;
  1249. if (likely(!context))
  1250. return 0;
  1251. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1252. if (!ax)
  1253. return -ENOMEM;
  1254. if (u_abs_timeout != NULL) {
  1255. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1256. kfree(ax);
  1257. return -EFAULT;
  1258. }
  1259. } else
  1260. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1261. ax->mqdes = mqdes;
  1262. ax->msg_len = msg_len;
  1263. ax->msg_prio = msg_prio;
  1264. ax->d.type = AUDIT_MQ_SENDRECV;
  1265. ax->d.next = context->aux;
  1266. context->aux = (void *)ax;
  1267. return 0;
  1268. }
  1269. /**
  1270. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1271. * @mqdes: MQ descriptor
  1272. * @msg_len: Message length
  1273. * @u_msg_prio: Message priority
  1274. * @u_abs_timeout: Message timeout in absolute time
  1275. *
  1276. * Returns 0 for success or NULL context or < 0 on error.
  1277. */
  1278. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1279. unsigned int __user *u_msg_prio,
  1280. const struct timespec __user *u_abs_timeout)
  1281. {
  1282. struct audit_aux_data_mq_sendrecv *ax;
  1283. struct audit_context *context = current->audit_context;
  1284. if (!audit_enabled)
  1285. return 0;
  1286. if (likely(!context))
  1287. return 0;
  1288. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1289. if (!ax)
  1290. return -ENOMEM;
  1291. if (u_msg_prio != NULL) {
  1292. if (get_user(ax->msg_prio, u_msg_prio)) {
  1293. kfree(ax);
  1294. return -EFAULT;
  1295. }
  1296. } else
  1297. ax->msg_prio = 0;
  1298. if (u_abs_timeout != NULL) {
  1299. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1300. kfree(ax);
  1301. return -EFAULT;
  1302. }
  1303. } else
  1304. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1305. ax->mqdes = mqdes;
  1306. ax->msg_len = msg_len;
  1307. ax->d.type = AUDIT_MQ_SENDRECV;
  1308. ax->d.next = context->aux;
  1309. context->aux = (void *)ax;
  1310. return 0;
  1311. }
  1312. /**
  1313. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1314. * @mqdes: MQ descriptor
  1315. * @u_notification: Notification event
  1316. *
  1317. * Returns 0 for success or NULL context or < 0 on error.
  1318. */
  1319. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1320. {
  1321. struct audit_aux_data_mq_notify *ax;
  1322. struct audit_context *context = current->audit_context;
  1323. if (!audit_enabled)
  1324. return 0;
  1325. if (likely(!context))
  1326. return 0;
  1327. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1328. if (!ax)
  1329. return -ENOMEM;
  1330. if (u_notification != NULL) {
  1331. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1332. kfree(ax);
  1333. return -EFAULT;
  1334. }
  1335. } else
  1336. memset(&ax->notification, 0, sizeof(ax->notification));
  1337. ax->mqdes = mqdes;
  1338. ax->d.type = AUDIT_MQ_NOTIFY;
  1339. ax->d.next = context->aux;
  1340. context->aux = (void *)ax;
  1341. return 0;
  1342. }
  1343. /**
  1344. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1345. * @mqdes: MQ descriptor
  1346. * @mqstat: MQ flags
  1347. *
  1348. * Returns 0 for success or NULL context or < 0 on error.
  1349. */
  1350. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1351. {
  1352. struct audit_aux_data_mq_getsetattr *ax;
  1353. struct audit_context *context = current->audit_context;
  1354. if (!audit_enabled)
  1355. return 0;
  1356. if (likely(!context))
  1357. return 0;
  1358. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1359. if (!ax)
  1360. return -ENOMEM;
  1361. ax->mqdes = mqdes;
  1362. ax->mqstat = *mqstat;
  1363. ax->d.type = AUDIT_MQ_GETSETATTR;
  1364. ax->d.next = context->aux;
  1365. context->aux = (void *)ax;
  1366. return 0;
  1367. }
  1368. /**
  1369. * audit_ipc_obj - record audit data for ipc object
  1370. * @ipcp: ipc permissions
  1371. *
  1372. * Returns 0 for success or NULL context or < 0 on error.
  1373. */
  1374. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1375. {
  1376. struct audit_aux_data_ipcctl *ax;
  1377. struct audit_context *context = current->audit_context;
  1378. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1379. if (!ax)
  1380. return -ENOMEM;
  1381. ax->uid = ipcp->uid;
  1382. ax->gid = ipcp->gid;
  1383. ax->mode = ipcp->mode;
  1384. selinux_get_ipc_sid(ipcp, &ax->osid);
  1385. ax->d.type = AUDIT_IPC;
  1386. ax->d.next = context->aux;
  1387. context->aux = (void *)ax;
  1388. return 0;
  1389. }
  1390. /**
  1391. * audit_ipc_set_perm - record audit data for new ipc permissions
  1392. * @qbytes: msgq bytes
  1393. * @uid: msgq user id
  1394. * @gid: msgq group id
  1395. * @mode: msgq mode (permissions)
  1396. *
  1397. * Returns 0 for success or NULL context or < 0 on error.
  1398. */
  1399. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1400. {
  1401. struct audit_aux_data_ipcctl *ax;
  1402. struct audit_context *context = current->audit_context;
  1403. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1404. if (!ax)
  1405. return -ENOMEM;
  1406. ax->qbytes = qbytes;
  1407. ax->uid = uid;
  1408. ax->gid = gid;
  1409. ax->mode = mode;
  1410. ax->d.type = AUDIT_IPC_SET_PERM;
  1411. ax->d.next = context->aux;
  1412. context->aux = (void *)ax;
  1413. return 0;
  1414. }
  1415. int audit_bprm(struct linux_binprm *bprm)
  1416. {
  1417. struct audit_aux_data_execve *ax;
  1418. struct audit_context *context = current->audit_context;
  1419. unsigned long p, next;
  1420. void *to;
  1421. if (likely(!audit_enabled || !context))
  1422. return 0;
  1423. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1424. GFP_KERNEL);
  1425. if (!ax)
  1426. return -ENOMEM;
  1427. ax->argc = bprm->argc;
  1428. ax->envc = bprm->envc;
  1429. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1430. struct page *page = bprm->page[p / PAGE_SIZE];
  1431. void *kaddr = kmap(page);
  1432. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1433. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1434. to += next - p;
  1435. kunmap(page);
  1436. }
  1437. ax->d.type = AUDIT_EXECVE;
  1438. ax->d.next = context->aux;
  1439. context->aux = (void *)ax;
  1440. return 0;
  1441. }
  1442. /**
  1443. * audit_socketcall - record audit data for sys_socketcall
  1444. * @nargs: number of args
  1445. * @args: args array
  1446. *
  1447. * Returns 0 for success or NULL context or < 0 on error.
  1448. */
  1449. int audit_socketcall(int nargs, unsigned long *args)
  1450. {
  1451. struct audit_aux_data_socketcall *ax;
  1452. struct audit_context *context = current->audit_context;
  1453. if (likely(!context))
  1454. return 0;
  1455. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1456. if (!ax)
  1457. return -ENOMEM;
  1458. ax->nargs = nargs;
  1459. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1460. ax->d.type = AUDIT_SOCKETCALL;
  1461. ax->d.next = context->aux;
  1462. context->aux = (void *)ax;
  1463. return 0;
  1464. }
  1465. /**
  1466. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1467. * @len: data length in user space
  1468. * @a: data address in kernel space
  1469. *
  1470. * Returns 0 for success or NULL context or < 0 on error.
  1471. */
  1472. int audit_sockaddr(int len, void *a)
  1473. {
  1474. struct audit_aux_data_sockaddr *ax;
  1475. struct audit_context *context = current->audit_context;
  1476. if (likely(!context))
  1477. return 0;
  1478. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1479. if (!ax)
  1480. return -ENOMEM;
  1481. ax->len = len;
  1482. memcpy(ax->a, a, len);
  1483. ax->d.type = AUDIT_SOCKADDR;
  1484. ax->d.next = context->aux;
  1485. context->aux = (void *)ax;
  1486. return 0;
  1487. }
  1488. /**
  1489. * audit_avc_path - record the granting or denial of permissions
  1490. * @dentry: dentry to record
  1491. * @mnt: mnt to record
  1492. *
  1493. * Returns 0 for success or NULL context or < 0 on error.
  1494. *
  1495. * Called from security/selinux/avc.c::avc_audit()
  1496. */
  1497. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1498. {
  1499. struct audit_aux_data_path *ax;
  1500. struct audit_context *context = current->audit_context;
  1501. if (likely(!context))
  1502. return 0;
  1503. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1504. if (!ax)
  1505. return -ENOMEM;
  1506. ax->dentry = dget(dentry);
  1507. ax->mnt = mntget(mnt);
  1508. ax->d.type = AUDIT_AVC_PATH;
  1509. ax->d.next = context->aux;
  1510. context->aux = (void *)ax;
  1511. return 0;
  1512. }
  1513. /**
  1514. * audit_signal_info - record signal info for shutting down audit subsystem
  1515. * @sig: signal value
  1516. * @t: task being signaled
  1517. *
  1518. * If the audit subsystem is being terminated, record the task (pid)
  1519. * and uid that is doing that.
  1520. */
  1521. void __audit_signal_info(int sig, struct task_struct *t)
  1522. {
  1523. extern pid_t audit_sig_pid;
  1524. extern uid_t audit_sig_uid;
  1525. extern u32 audit_sig_sid;
  1526. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1527. struct task_struct *tsk = current;
  1528. struct audit_context *ctx = tsk->audit_context;
  1529. audit_sig_pid = tsk->pid;
  1530. if (ctx)
  1531. audit_sig_uid = ctx->loginuid;
  1532. else
  1533. audit_sig_uid = tsk->uid;
  1534. selinux_get_task_sid(tsk, &audit_sig_sid);
  1535. }
  1536. }