auditsc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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_SUBJ_USER:
  299. case AUDIT_SUBJ_ROLE:
  300. case AUDIT_SUBJ_TYPE:
  301. case AUDIT_SUBJ_SEN:
  302. case AUDIT_SUBJ_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_OBJ_USER:
  320. case AUDIT_OBJ_ROLE:
  321. case AUDIT_OBJ_TYPE:
  322. case AUDIT_OBJ_LEV_LOW:
  323. case AUDIT_OBJ_LEV_HIGH:
  324. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  325. also applies here */
  326. if (f->se_rule) {
  327. /* Find files that match */
  328. if (name) {
  329. result = selinux_audit_rule_match(
  330. name->osid, f->type, f->op,
  331. f->se_rule, ctx);
  332. } else if (ctx) {
  333. for (j = 0; j < ctx->name_count; j++) {
  334. if (selinux_audit_rule_match(
  335. ctx->names[j].osid,
  336. f->type, f->op,
  337. f->se_rule, ctx)) {
  338. ++result;
  339. break;
  340. }
  341. }
  342. }
  343. /* Find ipc objects that match */
  344. if (ctx) {
  345. struct audit_aux_data *aux;
  346. for (aux = ctx->aux; aux;
  347. aux = aux->next) {
  348. if (aux->type == AUDIT_IPC) {
  349. struct audit_aux_data_ipcctl *axi = (void *)aux;
  350. if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
  351. ++result;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. break;
  359. case AUDIT_ARG0:
  360. case AUDIT_ARG1:
  361. case AUDIT_ARG2:
  362. case AUDIT_ARG3:
  363. if (ctx)
  364. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  365. break;
  366. case AUDIT_FILTERKEY:
  367. /* ignore this field for filtering */
  368. result = 1;
  369. break;
  370. }
  371. if (!result)
  372. return 0;
  373. }
  374. if (rule->filterkey)
  375. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  376. switch (rule->action) {
  377. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  378. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  379. }
  380. return 1;
  381. }
  382. /* At process creation time, we can determine if system-call auditing is
  383. * completely disabled for this task. Since we only have the task
  384. * structure at this point, we can only check uid and gid.
  385. */
  386. static enum audit_state audit_filter_task(struct task_struct *tsk)
  387. {
  388. struct audit_entry *e;
  389. enum audit_state state;
  390. rcu_read_lock();
  391. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  392. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  393. rcu_read_unlock();
  394. return state;
  395. }
  396. }
  397. rcu_read_unlock();
  398. return AUDIT_BUILD_CONTEXT;
  399. }
  400. /* At syscall entry and exit time, this filter is called if the
  401. * audit_state is not low enough that auditing cannot take place, but is
  402. * also not high enough that we already know we have to write an audit
  403. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  404. */
  405. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  406. struct audit_context *ctx,
  407. struct list_head *list)
  408. {
  409. struct audit_entry *e;
  410. enum audit_state state;
  411. if (audit_pid && tsk->tgid == audit_pid)
  412. return AUDIT_DISABLED;
  413. rcu_read_lock();
  414. if (!list_empty(list)) {
  415. int word = AUDIT_WORD(ctx->major);
  416. int bit = AUDIT_BIT(ctx->major);
  417. list_for_each_entry_rcu(e, list, list) {
  418. if ((e->rule.mask[word] & bit) == bit &&
  419. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  420. &state)) {
  421. rcu_read_unlock();
  422. return state;
  423. }
  424. }
  425. }
  426. rcu_read_unlock();
  427. return AUDIT_BUILD_CONTEXT;
  428. }
  429. /* At syscall exit time, this filter is called if any audit_names[] have been
  430. * collected during syscall processing. We only check rules in sublists at hash
  431. * buckets applicable to the inode numbers in audit_names[].
  432. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  433. */
  434. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  435. struct audit_context *ctx)
  436. {
  437. int i;
  438. struct audit_entry *e;
  439. enum audit_state state;
  440. if (audit_pid && tsk->tgid == audit_pid)
  441. return AUDIT_DISABLED;
  442. rcu_read_lock();
  443. for (i = 0; i < ctx->name_count; i++) {
  444. int word = AUDIT_WORD(ctx->major);
  445. int bit = AUDIT_BIT(ctx->major);
  446. struct audit_names *n = &ctx->names[i];
  447. int h = audit_hash_ino((u32)n->ino);
  448. struct list_head *list = &audit_inode_hash[h];
  449. if (list_empty(list))
  450. continue;
  451. list_for_each_entry_rcu(e, list, list) {
  452. if ((e->rule.mask[word] & bit) == bit &&
  453. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  454. rcu_read_unlock();
  455. return state;
  456. }
  457. }
  458. }
  459. rcu_read_unlock();
  460. return AUDIT_BUILD_CONTEXT;
  461. }
  462. void audit_set_auditable(struct audit_context *ctx)
  463. {
  464. ctx->auditable = 1;
  465. }
  466. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  467. int return_valid,
  468. int return_code)
  469. {
  470. struct audit_context *context = tsk->audit_context;
  471. if (likely(!context))
  472. return NULL;
  473. context->return_valid = return_valid;
  474. context->return_code = return_code;
  475. if (context->in_syscall && !context->auditable) {
  476. enum audit_state state;
  477. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  478. if (state == AUDIT_RECORD_CONTEXT) {
  479. context->auditable = 1;
  480. goto get_context;
  481. }
  482. state = audit_filter_inodes(tsk, context);
  483. if (state == AUDIT_RECORD_CONTEXT)
  484. context->auditable = 1;
  485. }
  486. get_context:
  487. context->pid = tsk->pid;
  488. context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
  489. context->uid = tsk->uid;
  490. context->gid = tsk->gid;
  491. context->euid = tsk->euid;
  492. context->suid = tsk->suid;
  493. context->fsuid = tsk->fsuid;
  494. context->egid = tsk->egid;
  495. context->sgid = tsk->sgid;
  496. context->fsgid = tsk->fsgid;
  497. context->personality = tsk->personality;
  498. tsk->audit_context = NULL;
  499. return context;
  500. }
  501. static inline void audit_free_names(struct audit_context *context)
  502. {
  503. int i;
  504. #if AUDIT_DEBUG == 2
  505. if (context->auditable
  506. ||context->put_count + context->ino_count != context->name_count) {
  507. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  508. " name_count=%d put_count=%d"
  509. " ino_count=%d [NOT freeing]\n",
  510. __FILE__, __LINE__,
  511. context->serial, context->major, context->in_syscall,
  512. context->name_count, context->put_count,
  513. context->ino_count);
  514. for (i = 0; i < context->name_count; i++) {
  515. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  516. context->names[i].name,
  517. context->names[i].name ?: "(null)");
  518. }
  519. dump_stack();
  520. return;
  521. }
  522. #endif
  523. #if AUDIT_DEBUG
  524. context->put_count = 0;
  525. context->ino_count = 0;
  526. #endif
  527. for (i = 0; i < context->name_count; i++) {
  528. if (context->names[i].name && context->names[i].name_put)
  529. __putname(context->names[i].name);
  530. }
  531. context->name_count = 0;
  532. if (context->pwd)
  533. dput(context->pwd);
  534. if (context->pwdmnt)
  535. mntput(context->pwdmnt);
  536. context->pwd = NULL;
  537. context->pwdmnt = NULL;
  538. }
  539. static inline void audit_free_aux(struct audit_context *context)
  540. {
  541. struct audit_aux_data *aux;
  542. while ((aux = context->aux)) {
  543. if (aux->type == AUDIT_AVC_PATH) {
  544. struct audit_aux_data_path *axi = (void *)aux;
  545. dput(axi->dentry);
  546. mntput(axi->mnt);
  547. }
  548. context->aux = aux->next;
  549. kfree(aux);
  550. }
  551. }
  552. static inline void audit_zero_context(struct audit_context *context,
  553. enum audit_state state)
  554. {
  555. uid_t loginuid = context->loginuid;
  556. memset(context, 0, sizeof(*context));
  557. context->state = state;
  558. context->loginuid = loginuid;
  559. }
  560. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  561. {
  562. struct audit_context *context;
  563. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  564. return NULL;
  565. audit_zero_context(context, state);
  566. return context;
  567. }
  568. /**
  569. * audit_alloc - allocate an audit context block for a task
  570. * @tsk: task
  571. *
  572. * Filter on the task information and allocate a per-task audit context
  573. * if necessary. Doing so turns on system call auditing for the
  574. * specified task. This is called from copy_process, so no lock is
  575. * needed.
  576. */
  577. int audit_alloc(struct task_struct *tsk)
  578. {
  579. struct audit_context *context;
  580. enum audit_state state;
  581. if (likely(!audit_enabled))
  582. return 0; /* Return if not auditing. */
  583. state = audit_filter_task(tsk);
  584. if (likely(state == AUDIT_DISABLED))
  585. return 0;
  586. if (!(context = audit_alloc_context(state))) {
  587. audit_log_lost("out of memory in audit_alloc");
  588. return -ENOMEM;
  589. }
  590. /* Preserve login uid */
  591. context->loginuid = -1;
  592. if (current->audit_context)
  593. context->loginuid = current->audit_context->loginuid;
  594. tsk->audit_context = context;
  595. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  596. return 0;
  597. }
  598. static inline void audit_free_context(struct audit_context *context)
  599. {
  600. struct audit_context *previous;
  601. int count = 0;
  602. do {
  603. previous = context->previous;
  604. if (previous || (count && count < 10)) {
  605. ++count;
  606. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  607. " freeing multiple contexts (%d)\n",
  608. context->serial, context->major,
  609. context->name_count, count);
  610. }
  611. audit_free_names(context);
  612. audit_free_aux(context);
  613. kfree(context->filterkey);
  614. kfree(context);
  615. context = previous;
  616. } while (context);
  617. if (count >= 10)
  618. printk(KERN_ERR "audit: freed %d contexts\n", count);
  619. }
  620. static void audit_log_task_context(struct audit_buffer *ab)
  621. {
  622. char *ctx = NULL;
  623. ssize_t len = 0;
  624. len = security_getprocattr(current, "current", NULL, 0);
  625. if (len < 0) {
  626. if (len != -EINVAL)
  627. goto error_path;
  628. return;
  629. }
  630. ctx = kmalloc(len, GFP_KERNEL);
  631. if (!ctx)
  632. goto error_path;
  633. len = security_getprocattr(current, "current", ctx, len);
  634. if (len < 0 )
  635. goto error_path;
  636. audit_log_format(ab, " subj=%s", ctx);
  637. return;
  638. error_path:
  639. kfree(ctx);
  640. audit_panic("error in audit_log_task_context");
  641. return;
  642. }
  643. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  644. {
  645. char name[sizeof(tsk->comm)];
  646. struct mm_struct *mm = tsk->mm;
  647. struct vm_area_struct *vma;
  648. /* tsk == current */
  649. get_task_comm(name, tsk);
  650. audit_log_format(ab, " comm=");
  651. audit_log_untrustedstring(ab, name);
  652. if (mm) {
  653. down_read(&mm->mmap_sem);
  654. vma = mm->mmap;
  655. while (vma) {
  656. if ((vma->vm_flags & VM_EXECUTABLE) &&
  657. vma->vm_file) {
  658. audit_log_d_path(ab, "exe=",
  659. vma->vm_file->f_dentry,
  660. vma->vm_file->f_vfsmnt);
  661. break;
  662. }
  663. vma = vma->vm_next;
  664. }
  665. up_read(&mm->mmap_sem);
  666. }
  667. audit_log_task_context(ab);
  668. }
  669. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  670. {
  671. int i, call_panic = 0;
  672. struct audit_buffer *ab;
  673. struct audit_aux_data *aux;
  674. const char *tty;
  675. /* tsk == current */
  676. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  677. if (!ab)
  678. return; /* audit_panic has been called */
  679. audit_log_format(ab, "arch=%x syscall=%d",
  680. context->arch, context->major);
  681. if (context->personality != PER_LINUX)
  682. audit_log_format(ab, " per=%lx", context->personality);
  683. if (context->return_valid)
  684. audit_log_format(ab, " success=%s exit=%ld",
  685. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  686. context->return_code);
  687. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  688. tty = tsk->signal->tty->name;
  689. else
  690. tty = "(none)";
  691. audit_log_format(ab,
  692. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  693. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  694. " euid=%u suid=%u fsuid=%u"
  695. " egid=%u sgid=%u fsgid=%u tty=%s",
  696. context->argv[0],
  697. context->argv[1],
  698. context->argv[2],
  699. context->argv[3],
  700. context->name_count,
  701. context->ppid,
  702. context->pid,
  703. context->loginuid,
  704. context->uid,
  705. context->gid,
  706. context->euid, context->suid, context->fsuid,
  707. context->egid, context->sgid, context->fsgid, tty);
  708. audit_log_task_info(ab, tsk);
  709. if (context->filterkey) {
  710. audit_log_format(ab, " key=");
  711. audit_log_untrustedstring(ab, context->filterkey);
  712. } else
  713. audit_log_format(ab, " key=(null)");
  714. audit_log_end(ab);
  715. for (aux = context->aux; aux; aux = aux->next) {
  716. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  717. if (!ab)
  718. continue; /* audit_panic has been called */
  719. switch (aux->type) {
  720. case AUDIT_MQ_OPEN: {
  721. struct audit_aux_data_mq_open *axi = (void *)aux;
  722. audit_log_format(ab,
  723. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  724. "mq_msgsize=%ld mq_curmsgs=%ld",
  725. axi->oflag, axi->mode, axi->attr.mq_flags,
  726. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  727. axi->attr.mq_curmsgs);
  728. break; }
  729. case AUDIT_MQ_SENDRECV: {
  730. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  731. audit_log_format(ab,
  732. "mqdes=%d msg_len=%zd msg_prio=%u "
  733. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  734. axi->mqdes, axi->msg_len, axi->msg_prio,
  735. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  736. break; }
  737. case AUDIT_MQ_NOTIFY: {
  738. struct audit_aux_data_mq_notify *axi = (void *)aux;
  739. audit_log_format(ab,
  740. "mqdes=%d sigev_signo=%d",
  741. axi->mqdes,
  742. axi->notification.sigev_signo);
  743. break; }
  744. case AUDIT_MQ_GETSETATTR: {
  745. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  746. audit_log_format(ab,
  747. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  748. "mq_curmsgs=%ld ",
  749. axi->mqdes,
  750. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  751. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  752. break; }
  753. case AUDIT_IPC: {
  754. struct audit_aux_data_ipcctl *axi = (void *)aux;
  755. audit_log_format(ab,
  756. "ouid=%u ogid=%u mode=%x",
  757. axi->uid, axi->gid, axi->mode);
  758. if (axi->osid != 0) {
  759. char *ctx = NULL;
  760. u32 len;
  761. if (selinux_ctxid_to_string(
  762. axi->osid, &ctx, &len)) {
  763. audit_log_format(ab, " osid=%u",
  764. axi->osid);
  765. call_panic = 1;
  766. } else
  767. audit_log_format(ab, " obj=%s", ctx);
  768. kfree(ctx);
  769. }
  770. break; }
  771. case AUDIT_IPC_SET_PERM: {
  772. struct audit_aux_data_ipcctl *axi = (void *)aux;
  773. audit_log_format(ab,
  774. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  775. axi->qbytes, axi->uid, axi->gid, axi->mode);
  776. break; }
  777. case AUDIT_EXECVE: {
  778. struct audit_aux_data_execve *axi = (void *)aux;
  779. int i;
  780. const char *p;
  781. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  782. audit_log_format(ab, "a%d=", i);
  783. p = audit_log_untrustedstring(ab, p);
  784. audit_log_format(ab, "\n");
  785. }
  786. break; }
  787. case AUDIT_SOCKETCALL: {
  788. int i;
  789. struct audit_aux_data_socketcall *axs = (void *)aux;
  790. audit_log_format(ab, "nargs=%d", axs->nargs);
  791. for (i=0; i<axs->nargs; i++)
  792. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  793. break; }
  794. case AUDIT_SOCKADDR: {
  795. struct audit_aux_data_sockaddr *axs = (void *)aux;
  796. audit_log_format(ab, "saddr=");
  797. audit_log_hex(ab, axs->a, axs->len);
  798. break; }
  799. case AUDIT_AVC_PATH: {
  800. struct audit_aux_data_path *axi = (void *)aux;
  801. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  802. break; }
  803. }
  804. audit_log_end(ab);
  805. }
  806. if (context->pwd && context->pwdmnt) {
  807. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  808. if (ab) {
  809. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  810. audit_log_end(ab);
  811. }
  812. }
  813. for (i = 0; i < context->name_count; i++) {
  814. struct audit_names *n = &context->names[i];
  815. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  816. if (!ab)
  817. continue; /* audit_panic has been called */
  818. audit_log_format(ab, "item=%d", i);
  819. if (n->name) {
  820. switch(n->name_len) {
  821. case AUDIT_NAME_FULL:
  822. /* log the full path */
  823. audit_log_format(ab, " name=");
  824. audit_log_untrustedstring(ab, n->name);
  825. break;
  826. case 0:
  827. /* name was specified as a relative path and the
  828. * directory component is the cwd */
  829. audit_log_d_path(ab, " name=", context->pwd,
  830. context->pwdmnt);
  831. break;
  832. default:
  833. /* log the name's directory component */
  834. audit_log_format(ab, " name=");
  835. audit_log_n_untrustedstring(ab, n->name_len,
  836. n->name);
  837. }
  838. } else
  839. audit_log_format(ab, " name=(null)");
  840. if (n->ino != (unsigned long)-1) {
  841. audit_log_format(ab, " inode=%lu"
  842. " dev=%02x:%02x mode=%#o"
  843. " ouid=%u ogid=%u rdev=%02x:%02x",
  844. n->ino,
  845. MAJOR(n->dev),
  846. MINOR(n->dev),
  847. n->mode,
  848. n->uid,
  849. n->gid,
  850. MAJOR(n->rdev),
  851. MINOR(n->rdev));
  852. }
  853. if (n->osid != 0) {
  854. char *ctx = NULL;
  855. u32 len;
  856. if (selinux_ctxid_to_string(
  857. n->osid, &ctx, &len)) {
  858. audit_log_format(ab, " osid=%u", n->osid);
  859. call_panic = 2;
  860. } else
  861. audit_log_format(ab, " obj=%s", ctx);
  862. kfree(ctx);
  863. }
  864. audit_log_end(ab);
  865. }
  866. if (call_panic)
  867. audit_panic("error converting sid to string");
  868. }
  869. /**
  870. * audit_free - free a per-task audit context
  871. * @tsk: task whose audit context block to free
  872. *
  873. * Called from copy_process and do_exit
  874. */
  875. void audit_free(struct task_struct *tsk)
  876. {
  877. struct audit_context *context;
  878. context = audit_get_context(tsk, 0, 0);
  879. if (likely(!context))
  880. return;
  881. /* Check for system calls that do not go through the exit
  882. * function (e.g., exit_group), then free context block.
  883. * We use GFP_ATOMIC here because we might be doing this
  884. * in the context of the idle thread */
  885. /* that can happen only if we are called from do_exit() */
  886. if (context->in_syscall && context->auditable)
  887. audit_log_exit(context, tsk);
  888. audit_free_context(context);
  889. }
  890. /**
  891. * audit_syscall_entry - fill in an audit record at syscall entry
  892. * @tsk: task being audited
  893. * @arch: architecture type
  894. * @major: major syscall type (function)
  895. * @a1: additional syscall register 1
  896. * @a2: additional syscall register 2
  897. * @a3: additional syscall register 3
  898. * @a4: additional syscall register 4
  899. *
  900. * Fill in audit context at syscall entry. This only happens if the
  901. * audit context was created when the task was created and the state or
  902. * filters demand the audit context be built. If the state from the
  903. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  904. * then the record will be written at syscall exit time (otherwise, it
  905. * will only be written if another part of the kernel requests that it
  906. * be written).
  907. */
  908. void audit_syscall_entry(int arch, int major,
  909. unsigned long a1, unsigned long a2,
  910. unsigned long a3, unsigned long a4)
  911. {
  912. struct task_struct *tsk = current;
  913. struct audit_context *context = tsk->audit_context;
  914. enum audit_state state;
  915. BUG_ON(!context);
  916. /*
  917. * This happens only on certain architectures that make system
  918. * calls in kernel_thread via the entry.S interface, instead of
  919. * with direct calls. (If you are porting to a new
  920. * architecture, hitting this condition can indicate that you
  921. * got the _exit/_leave calls backward in entry.S.)
  922. *
  923. * i386 no
  924. * x86_64 no
  925. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  926. *
  927. * This also happens with vm86 emulation in a non-nested manner
  928. * (entries without exits), so this case must be caught.
  929. */
  930. if (context->in_syscall) {
  931. struct audit_context *newctx;
  932. #if AUDIT_DEBUG
  933. printk(KERN_ERR
  934. "audit(:%d) pid=%d in syscall=%d;"
  935. " entering syscall=%d\n",
  936. context->serial, tsk->pid, context->major, major);
  937. #endif
  938. newctx = audit_alloc_context(context->state);
  939. if (newctx) {
  940. newctx->previous = context;
  941. context = newctx;
  942. tsk->audit_context = newctx;
  943. } else {
  944. /* If we can't alloc a new context, the best we
  945. * can do is to leak memory (any pending putname
  946. * will be lost). The only other alternative is
  947. * to abandon auditing. */
  948. audit_zero_context(context, context->state);
  949. }
  950. }
  951. BUG_ON(context->in_syscall || context->name_count);
  952. if (!audit_enabled)
  953. return;
  954. context->arch = arch;
  955. context->major = major;
  956. context->argv[0] = a1;
  957. context->argv[1] = a2;
  958. context->argv[2] = a3;
  959. context->argv[3] = a4;
  960. state = context->state;
  961. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  962. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  963. if (likely(state == AUDIT_DISABLED))
  964. return;
  965. context->serial = 0;
  966. context->ctime = CURRENT_TIME;
  967. context->in_syscall = 1;
  968. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  969. }
  970. /**
  971. * audit_syscall_exit - deallocate audit context after a system call
  972. * @tsk: task being audited
  973. * @valid: success/failure flag
  974. * @return_code: syscall return value
  975. *
  976. * Tear down after system call. If the audit context has been marked as
  977. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  978. * filtering, or because some other part of the kernel write an audit
  979. * message), then write out the syscall information. In call cases,
  980. * free the names stored from getname().
  981. */
  982. void audit_syscall_exit(int valid, long return_code)
  983. {
  984. struct task_struct *tsk = current;
  985. struct audit_context *context;
  986. context = audit_get_context(tsk, valid, return_code);
  987. if (likely(!context))
  988. return;
  989. if (context->in_syscall && context->auditable)
  990. audit_log_exit(context, tsk);
  991. context->in_syscall = 0;
  992. context->auditable = 0;
  993. if (context->previous) {
  994. struct audit_context *new_context = context->previous;
  995. context->previous = NULL;
  996. audit_free_context(context);
  997. tsk->audit_context = new_context;
  998. } else {
  999. audit_free_names(context);
  1000. audit_free_aux(context);
  1001. kfree(context->filterkey);
  1002. context->filterkey = NULL;
  1003. tsk->audit_context = context;
  1004. }
  1005. }
  1006. /**
  1007. * audit_getname - add a name to the list
  1008. * @name: name to add
  1009. *
  1010. * Add a name to the list of audit names for this context.
  1011. * Called from fs/namei.c:getname().
  1012. */
  1013. void __audit_getname(const char *name)
  1014. {
  1015. struct audit_context *context = current->audit_context;
  1016. if (IS_ERR(name) || !name)
  1017. return;
  1018. if (!context->in_syscall) {
  1019. #if AUDIT_DEBUG == 2
  1020. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1021. __FILE__, __LINE__, context->serial, name);
  1022. dump_stack();
  1023. #endif
  1024. return;
  1025. }
  1026. BUG_ON(context->name_count >= AUDIT_NAMES);
  1027. context->names[context->name_count].name = name;
  1028. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1029. context->names[context->name_count].name_put = 1;
  1030. context->names[context->name_count].ino = (unsigned long)-1;
  1031. ++context->name_count;
  1032. if (!context->pwd) {
  1033. read_lock(&current->fs->lock);
  1034. context->pwd = dget(current->fs->pwd);
  1035. context->pwdmnt = mntget(current->fs->pwdmnt);
  1036. read_unlock(&current->fs->lock);
  1037. }
  1038. }
  1039. /* audit_putname - intercept a putname request
  1040. * @name: name to intercept and delay for putname
  1041. *
  1042. * If we have stored the name from getname in the audit context,
  1043. * then we delay the putname until syscall exit.
  1044. * Called from include/linux/fs.h:putname().
  1045. */
  1046. void audit_putname(const char *name)
  1047. {
  1048. struct audit_context *context = current->audit_context;
  1049. BUG_ON(!context);
  1050. if (!context->in_syscall) {
  1051. #if AUDIT_DEBUG == 2
  1052. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1053. __FILE__, __LINE__, context->serial, name);
  1054. if (context->name_count) {
  1055. int i;
  1056. for (i = 0; i < context->name_count; i++)
  1057. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1058. context->names[i].name,
  1059. context->names[i].name ?: "(null)");
  1060. }
  1061. #endif
  1062. __putname(name);
  1063. }
  1064. #if AUDIT_DEBUG
  1065. else {
  1066. ++context->put_count;
  1067. if (context->put_count > context->name_count) {
  1068. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1069. " in_syscall=%d putname(%p) name_count=%d"
  1070. " put_count=%d\n",
  1071. __FILE__, __LINE__,
  1072. context->serial, context->major,
  1073. context->in_syscall, name, context->name_count,
  1074. context->put_count);
  1075. dump_stack();
  1076. }
  1077. }
  1078. #endif
  1079. }
  1080. /* Copy inode data into an audit_names. */
  1081. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1082. {
  1083. name->ino = inode->i_ino;
  1084. name->dev = inode->i_sb->s_dev;
  1085. name->mode = inode->i_mode;
  1086. name->uid = inode->i_uid;
  1087. name->gid = inode->i_gid;
  1088. name->rdev = inode->i_rdev;
  1089. selinux_get_inode_sid(inode, &name->osid);
  1090. }
  1091. /**
  1092. * audit_inode - store the inode and device from a lookup
  1093. * @name: name being audited
  1094. * @inode: inode being audited
  1095. *
  1096. * Called from fs/namei.c:path_lookup().
  1097. */
  1098. void __audit_inode(const char *name, const struct inode *inode)
  1099. {
  1100. int idx;
  1101. struct audit_context *context = current->audit_context;
  1102. if (!context->in_syscall)
  1103. return;
  1104. if (context->name_count
  1105. && context->names[context->name_count-1].name
  1106. && context->names[context->name_count-1].name == name)
  1107. idx = context->name_count - 1;
  1108. else if (context->name_count > 1
  1109. && context->names[context->name_count-2].name
  1110. && context->names[context->name_count-2].name == name)
  1111. idx = context->name_count - 2;
  1112. else {
  1113. /* FIXME: how much do we care about inodes that have no
  1114. * associated name? */
  1115. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1116. return;
  1117. idx = context->name_count++;
  1118. context->names[idx].name = NULL;
  1119. #if AUDIT_DEBUG
  1120. ++context->ino_count;
  1121. #endif
  1122. }
  1123. audit_copy_inode(&context->names[idx], inode);
  1124. }
  1125. /**
  1126. * audit_inode_child - collect inode info for created/removed objects
  1127. * @dname: inode's dentry name
  1128. * @inode: inode being audited
  1129. * @parent: inode of dentry parent
  1130. *
  1131. * For syscalls that create or remove filesystem objects, audit_inode
  1132. * can only collect information for the filesystem object's parent.
  1133. * This call updates the audit context with the child's information.
  1134. * Syscalls that create a new filesystem object must be hooked after
  1135. * the object is created. Syscalls that remove a filesystem object
  1136. * must be hooked prior, in order to capture the target inode during
  1137. * unsuccessful attempts.
  1138. */
  1139. void __audit_inode_child(const char *dname, const struct inode *inode,
  1140. const struct inode *parent)
  1141. {
  1142. int idx;
  1143. struct audit_context *context = current->audit_context;
  1144. const char *found_name = NULL;
  1145. int dirlen = 0;
  1146. if (!context->in_syscall)
  1147. return;
  1148. /* determine matching parent */
  1149. if (!dname)
  1150. goto update_context;
  1151. for (idx = 0; idx < context->name_count; idx++)
  1152. if (context->names[idx].ino == parent->i_ino) {
  1153. const char *name = context->names[idx].name;
  1154. if (!name)
  1155. continue;
  1156. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1157. context->names[idx].name_len = dirlen;
  1158. found_name = name;
  1159. break;
  1160. }
  1161. }
  1162. update_context:
  1163. idx = context->name_count++;
  1164. #if AUDIT_DEBUG
  1165. context->ino_count++;
  1166. #endif
  1167. /* Re-use the name belonging to the slot for a matching parent directory.
  1168. * All names for this context are relinquished in audit_free_names() */
  1169. context->names[idx].name = found_name;
  1170. context->names[idx].name_len = AUDIT_NAME_FULL;
  1171. context->names[idx].name_put = 0; /* don't call __putname() */
  1172. if (!inode)
  1173. context->names[idx].ino = (unsigned long)-1;
  1174. else
  1175. audit_copy_inode(&context->names[idx], inode);
  1176. /* A parent was not found in audit_names, so copy the inode data for the
  1177. * provided parent. */
  1178. if (!found_name) {
  1179. idx = context->name_count++;
  1180. #if AUDIT_DEBUG
  1181. context->ino_count++;
  1182. #endif
  1183. audit_copy_inode(&context->names[idx], parent);
  1184. }
  1185. }
  1186. /**
  1187. * audit_inode_update - update inode info for last collected name
  1188. * @inode: inode being audited
  1189. *
  1190. * When open() is called on an existing object with the O_CREAT flag, the inode
  1191. * data audit initially collects is incorrect. This additional hook ensures
  1192. * audit has the inode data for the actual object to be opened.
  1193. */
  1194. void __audit_inode_update(const struct inode *inode)
  1195. {
  1196. struct audit_context *context = current->audit_context;
  1197. int idx;
  1198. if (!context->in_syscall || !inode)
  1199. return;
  1200. if (context->name_count == 0) {
  1201. context->name_count++;
  1202. #if AUDIT_DEBUG
  1203. context->ino_count++;
  1204. #endif
  1205. }
  1206. idx = context->name_count - 1;
  1207. audit_copy_inode(&context->names[idx], inode);
  1208. }
  1209. /**
  1210. * auditsc_get_stamp - get local copies of audit_context values
  1211. * @ctx: audit_context for the task
  1212. * @t: timespec to store time recorded in the audit_context
  1213. * @serial: serial value that is recorded in the audit_context
  1214. *
  1215. * Also sets the context as auditable.
  1216. */
  1217. void auditsc_get_stamp(struct audit_context *ctx,
  1218. struct timespec *t, unsigned int *serial)
  1219. {
  1220. if (!ctx->serial)
  1221. ctx->serial = audit_serial();
  1222. t->tv_sec = ctx->ctime.tv_sec;
  1223. t->tv_nsec = ctx->ctime.tv_nsec;
  1224. *serial = ctx->serial;
  1225. ctx->auditable = 1;
  1226. }
  1227. /**
  1228. * audit_set_loginuid - set a task's audit_context loginuid
  1229. * @task: task whose audit context is being modified
  1230. * @loginuid: loginuid value
  1231. *
  1232. * Returns 0.
  1233. *
  1234. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1235. */
  1236. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1237. {
  1238. struct audit_context *context = task->audit_context;
  1239. if (context) {
  1240. /* Only log if audit is enabled */
  1241. if (context->in_syscall) {
  1242. struct audit_buffer *ab;
  1243. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1244. if (ab) {
  1245. audit_log_format(ab, "login pid=%d uid=%u "
  1246. "old auid=%u new auid=%u",
  1247. task->pid, task->uid,
  1248. context->loginuid, loginuid);
  1249. audit_log_end(ab);
  1250. }
  1251. }
  1252. context->loginuid = loginuid;
  1253. }
  1254. return 0;
  1255. }
  1256. /**
  1257. * audit_get_loginuid - get the loginuid for an audit_context
  1258. * @ctx: the audit_context
  1259. *
  1260. * Returns the context's loginuid or -1 if @ctx is NULL.
  1261. */
  1262. uid_t audit_get_loginuid(struct audit_context *ctx)
  1263. {
  1264. return ctx ? ctx->loginuid : -1;
  1265. }
  1266. /**
  1267. * __audit_mq_open - record audit data for a POSIX MQ open
  1268. * @oflag: open flag
  1269. * @mode: mode bits
  1270. * @u_attr: queue attributes
  1271. *
  1272. * Returns 0 for success or NULL context or < 0 on error.
  1273. */
  1274. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1275. {
  1276. struct audit_aux_data_mq_open *ax;
  1277. struct audit_context *context = current->audit_context;
  1278. if (!audit_enabled)
  1279. return 0;
  1280. if (likely(!context))
  1281. return 0;
  1282. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1283. if (!ax)
  1284. return -ENOMEM;
  1285. if (u_attr != NULL) {
  1286. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1287. kfree(ax);
  1288. return -EFAULT;
  1289. }
  1290. } else
  1291. memset(&ax->attr, 0, sizeof(ax->attr));
  1292. ax->oflag = oflag;
  1293. ax->mode = mode;
  1294. ax->d.type = AUDIT_MQ_OPEN;
  1295. ax->d.next = context->aux;
  1296. context->aux = (void *)ax;
  1297. return 0;
  1298. }
  1299. /**
  1300. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1301. * @mqdes: MQ descriptor
  1302. * @msg_len: Message length
  1303. * @msg_prio: Message priority
  1304. * @u_abs_timeout: Message timeout in absolute time
  1305. *
  1306. * Returns 0 for success or NULL context or < 0 on error.
  1307. */
  1308. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1309. const struct timespec __user *u_abs_timeout)
  1310. {
  1311. struct audit_aux_data_mq_sendrecv *ax;
  1312. struct audit_context *context = current->audit_context;
  1313. if (!audit_enabled)
  1314. return 0;
  1315. if (likely(!context))
  1316. return 0;
  1317. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1318. if (!ax)
  1319. return -ENOMEM;
  1320. if (u_abs_timeout != NULL) {
  1321. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1322. kfree(ax);
  1323. return -EFAULT;
  1324. }
  1325. } else
  1326. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1327. ax->mqdes = mqdes;
  1328. ax->msg_len = msg_len;
  1329. ax->msg_prio = msg_prio;
  1330. ax->d.type = AUDIT_MQ_SENDRECV;
  1331. ax->d.next = context->aux;
  1332. context->aux = (void *)ax;
  1333. return 0;
  1334. }
  1335. /**
  1336. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1337. * @mqdes: MQ descriptor
  1338. * @msg_len: Message length
  1339. * @u_msg_prio: Message priority
  1340. * @u_abs_timeout: Message timeout in absolute time
  1341. *
  1342. * Returns 0 for success or NULL context or < 0 on error.
  1343. */
  1344. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1345. unsigned int __user *u_msg_prio,
  1346. const struct timespec __user *u_abs_timeout)
  1347. {
  1348. struct audit_aux_data_mq_sendrecv *ax;
  1349. struct audit_context *context = current->audit_context;
  1350. if (!audit_enabled)
  1351. return 0;
  1352. if (likely(!context))
  1353. return 0;
  1354. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1355. if (!ax)
  1356. return -ENOMEM;
  1357. if (u_msg_prio != NULL) {
  1358. if (get_user(ax->msg_prio, u_msg_prio)) {
  1359. kfree(ax);
  1360. return -EFAULT;
  1361. }
  1362. } else
  1363. ax->msg_prio = 0;
  1364. if (u_abs_timeout != NULL) {
  1365. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1366. kfree(ax);
  1367. return -EFAULT;
  1368. }
  1369. } else
  1370. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1371. ax->mqdes = mqdes;
  1372. ax->msg_len = msg_len;
  1373. ax->d.type = AUDIT_MQ_SENDRECV;
  1374. ax->d.next = context->aux;
  1375. context->aux = (void *)ax;
  1376. return 0;
  1377. }
  1378. /**
  1379. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1380. * @mqdes: MQ descriptor
  1381. * @u_notification: Notification event
  1382. *
  1383. * Returns 0 for success or NULL context or < 0 on error.
  1384. */
  1385. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1386. {
  1387. struct audit_aux_data_mq_notify *ax;
  1388. struct audit_context *context = current->audit_context;
  1389. if (!audit_enabled)
  1390. return 0;
  1391. if (likely(!context))
  1392. return 0;
  1393. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1394. if (!ax)
  1395. return -ENOMEM;
  1396. if (u_notification != NULL) {
  1397. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1398. kfree(ax);
  1399. return -EFAULT;
  1400. }
  1401. } else
  1402. memset(&ax->notification, 0, sizeof(ax->notification));
  1403. ax->mqdes = mqdes;
  1404. ax->d.type = AUDIT_MQ_NOTIFY;
  1405. ax->d.next = context->aux;
  1406. context->aux = (void *)ax;
  1407. return 0;
  1408. }
  1409. /**
  1410. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1411. * @mqdes: MQ descriptor
  1412. * @mqstat: MQ flags
  1413. *
  1414. * Returns 0 for success or NULL context or < 0 on error.
  1415. */
  1416. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1417. {
  1418. struct audit_aux_data_mq_getsetattr *ax;
  1419. struct audit_context *context = current->audit_context;
  1420. if (!audit_enabled)
  1421. return 0;
  1422. if (likely(!context))
  1423. return 0;
  1424. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1425. if (!ax)
  1426. return -ENOMEM;
  1427. ax->mqdes = mqdes;
  1428. ax->mqstat = *mqstat;
  1429. ax->d.type = AUDIT_MQ_GETSETATTR;
  1430. ax->d.next = context->aux;
  1431. context->aux = (void *)ax;
  1432. return 0;
  1433. }
  1434. /**
  1435. * audit_ipc_obj - record audit data for ipc object
  1436. * @ipcp: ipc permissions
  1437. *
  1438. * Returns 0 for success or NULL context or < 0 on error.
  1439. */
  1440. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1441. {
  1442. struct audit_aux_data_ipcctl *ax;
  1443. struct audit_context *context = current->audit_context;
  1444. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1445. if (!ax)
  1446. return -ENOMEM;
  1447. ax->uid = ipcp->uid;
  1448. ax->gid = ipcp->gid;
  1449. ax->mode = ipcp->mode;
  1450. selinux_get_ipc_sid(ipcp, &ax->osid);
  1451. ax->d.type = AUDIT_IPC;
  1452. ax->d.next = context->aux;
  1453. context->aux = (void *)ax;
  1454. return 0;
  1455. }
  1456. /**
  1457. * audit_ipc_set_perm - record audit data for new ipc permissions
  1458. * @qbytes: msgq bytes
  1459. * @uid: msgq user id
  1460. * @gid: msgq group id
  1461. * @mode: msgq mode (permissions)
  1462. *
  1463. * Returns 0 for success or NULL context or < 0 on error.
  1464. */
  1465. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1466. {
  1467. struct audit_aux_data_ipcctl *ax;
  1468. struct audit_context *context = current->audit_context;
  1469. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1470. if (!ax)
  1471. return -ENOMEM;
  1472. ax->qbytes = qbytes;
  1473. ax->uid = uid;
  1474. ax->gid = gid;
  1475. ax->mode = mode;
  1476. ax->d.type = AUDIT_IPC_SET_PERM;
  1477. ax->d.next = context->aux;
  1478. context->aux = (void *)ax;
  1479. return 0;
  1480. }
  1481. int audit_bprm(struct linux_binprm *bprm)
  1482. {
  1483. struct audit_aux_data_execve *ax;
  1484. struct audit_context *context = current->audit_context;
  1485. unsigned long p, next;
  1486. void *to;
  1487. if (likely(!audit_enabled || !context))
  1488. return 0;
  1489. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1490. GFP_KERNEL);
  1491. if (!ax)
  1492. return -ENOMEM;
  1493. ax->argc = bprm->argc;
  1494. ax->envc = bprm->envc;
  1495. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1496. struct page *page = bprm->page[p / PAGE_SIZE];
  1497. void *kaddr = kmap(page);
  1498. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1499. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1500. to += next - p;
  1501. kunmap(page);
  1502. }
  1503. ax->d.type = AUDIT_EXECVE;
  1504. ax->d.next = context->aux;
  1505. context->aux = (void *)ax;
  1506. return 0;
  1507. }
  1508. /**
  1509. * audit_socketcall - record audit data for sys_socketcall
  1510. * @nargs: number of args
  1511. * @args: args array
  1512. *
  1513. * Returns 0 for success or NULL context or < 0 on error.
  1514. */
  1515. int audit_socketcall(int nargs, unsigned long *args)
  1516. {
  1517. struct audit_aux_data_socketcall *ax;
  1518. struct audit_context *context = current->audit_context;
  1519. if (likely(!context))
  1520. return 0;
  1521. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1522. if (!ax)
  1523. return -ENOMEM;
  1524. ax->nargs = nargs;
  1525. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1526. ax->d.type = AUDIT_SOCKETCALL;
  1527. ax->d.next = context->aux;
  1528. context->aux = (void *)ax;
  1529. return 0;
  1530. }
  1531. /**
  1532. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1533. * @len: data length in user space
  1534. * @a: data address in kernel space
  1535. *
  1536. * Returns 0 for success or NULL context or < 0 on error.
  1537. */
  1538. int audit_sockaddr(int len, void *a)
  1539. {
  1540. struct audit_aux_data_sockaddr *ax;
  1541. struct audit_context *context = current->audit_context;
  1542. if (likely(!context))
  1543. return 0;
  1544. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1545. if (!ax)
  1546. return -ENOMEM;
  1547. ax->len = len;
  1548. memcpy(ax->a, a, len);
  1549. ax->d.type = AUDIT_SOCKADDR;
  1550. ax->d.next = context->aux;
  1551. context->aux = (void *)ax;
  1552. return 0;
  1553. }
  1554. /**
  1555. * audit_avc_path - record the granting or denial of permissions
  1556. * @dentry: dentry to record
  1557. * @mnt: mnt to record
  1558. *
  1559. * Returns 0 for success or NULL context or < 0 on error.
  1560. *
  1561. * Called from security/selinux/avc.c::avc_audit()
  1562. */
  1563. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1564. {
  1565. struct audit_aux_data_path *ax;
  1566. struct audit_context *context = current->audit_context;
  1567. if (likely(!context))
  1568. return 0;
  1569. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1570. if (!ax)
  1571. return -ENOMEM;
  1572. ax->dentry = dget(dentry);
  1573. ax->mnt = mntget(mnt);
  1574. ax->d.type = AUDIT_AVC_PATH;
  1575. ax->d.next = context->aux;
  1576. context->aux = (void *)ax;
  1577. return 0;
  1578. }
  1579. /**
  1580. * audit_signal_info - record signal info for shutting down audit subsystem
  1581. * @sig: signal value
  1582. * @t: task being signaled
  1583. *
  1584. * If the audit subsystem is being terminated, record the task (pid)
  1585. * and uid that is doing that.
  1586. */
  1587. void __audit_signal_info(int sig, struct task_struct *t)
  1588. {
  1589. extern pid_t audit_sig_pid;
  1590. extern uid_t audit_sig_uid;
  1591. extern u32 audit_sig_sid;
  1592. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1593. struct task_struct *tsk = current;
  1594. struct audit_context *ctx = tsk->audit_context;
  1595. audit_sig_pid = tsk->pid;
  1596. if (ctx)
  1597. audit_sig_uid = ctx->loginuid;
  1598. else
  1599. audit_sig_uid = tsk->uid;
  1600. selinux_get_task_sid(tsk, &audit_sig_sid);
  1601. }
  1602. }