auditsc.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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 <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/mm.h>
  50. #include <linux/module.h>
  51. #include <linux/mount.h>
  52. #include <linux/socket.h>
  53. #include <linux/mqueue.h>
  54. #include <linux/audit.h>
  55. #include <linux/personality.h>
  56. #include <linux/time.h>
  57. #include <linux/netlink.h>
  58. #include <linux/compiler.h>
  59. #include <asm/unistd.h>
  60. #include <linux/security.h>
  61. #include <linux/list.h>
  62. #include <linux/tty.h>
  63. #include <linux/selinux.h>
  64. #include <linux/binfmts.h>
  65. #include <linux/highmem.h>
  66. #include <linux/syscalls.h>
  67. #include <linux/inotify.h>
  68. #include "audit.h"
  69. extern struct list_head audit_filter_list[];
  70. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  71. * for saving names from getname(). */
  72. #define AUDIT_NAMES 20
  73. /* Indicates that audit should log the full pathname. */
  74. #define AUDIT_NAME_FULL -1
  75. /* number of audit rules */
  76. int audit_n_rules;
  77. /* determines whether we collect data for signals sent */
  78. int audit_signals;
  79. /* When fs/namei.c:getname() is called, we store the pointer in name and
  80. * we don't let putname() free it (instead we free all of the saved
  81. * pointers at syscall exit time).
  82. *
  83. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  84. struct audit_names {
  85. const char *name;
  86. int name_len; /* number of name's characters to log */
  87. unsigned name_put; /* call __putname() for this name */
  88. unsigned long ino;
  89. dev_t dev;
  90. umode_t mode;
  91. uid_t uid;
  92. gid_t gid;
  93. dev_t rdev;
  94. u32 osid;
  95. };
  96. struct audit_aux_data {
  97. struct audit_aux_data *next;
  98. int type;
  99. };
  100. #define AUDIT_AUX_IPCPERM 0
  101. /* Number of target pids per aux struct. */
  102. #define AUDIT_AUX_PIDS 16
  103. struct audit_aux_data_mq_open {
  104. struct audit_aux_data d;
  105. int oflag;
  106. mode_t mode;
  107. struct mq_attr attr;
  108. };
  109. struct audit_aux_data_mq_sendrecv {
  110. struct audit_aux_data d;
  111. mqd_t mqdes;
  112. size_t msg_len;
  113. unsigned int msg_prio;
  114. struct timespec abs_timeout;
  115. };
  116. struct audit_aux_data_mq_notify {
  117. struct audit_aux_data d;
  118. mqd_t mqdes;
  119. struct sigevent notification;
  120. };
  121. struct audit_aux_data_mq_getsetattr {
  122. struct audit_aux_data d;
  123. mqd_t mqdes;
  124. struct mq_attr mqstat;
  125. };
  126. struct audit_aux_data_ipcctl {
  127. struct audit_aux_data d;
  128. struct ipc_perm p;
  129. unsigned long qbytes;
  130. uid_t uid;
  131. gid_t gid;
  132. mode_t mode;
  133. u32 osid;
  134. };
  135. struct audit_aux_data_execve {
  136. struct audit_aux_data d;
  137. int argc;
  138. int envc;
  139. struct mm_struct *mm;
  140. };
  141. struct audit_aux_data_socketcall {
  142. struct audit_aux_data d;
  143. int nargs;
  144. unsigned long args[0];
  145. };
  146. struct audit_aux_data_sockaddr {
  147. struct audit_aux_data d;
  148. int len;
  149. char a[0];
  150. };
  151. struct audit_aux_data_fd_pair {
  152. struct audit_aux_data d;
  153. int fd[2];
  154. };
  155. struct audit_aux_data_pids {
  156. struct audit_aux_data d;
  157. pid_t target_pid[AUDIT_AUX_PIDS];
  158. u32 target_sid[AUDIT_AUX_PIDS];
  159. int pid_count;
  160. };
  161. struct audit_tree_refs {
  162. struct audit_tree_refs *next;
  163. struct audit_chunk *c[31];
  164. };
  165. /* The per-task audit context. */
  166. struct audit_context {
  167. int dummy; /* must be the first element */
  168. int in_syscall; /* 1 if task is in a syscall */
  169. enum audit_state state;
  170. unsigned int serial; /* serial number for record */
  171. struct timespec ctime; /* time of syscall entry */
  172. uid_t loginuid; /* login uid (identity) */
  173. int major; /* syscall number */
  174. unsigned long argv[4]; /* syscall arguments */
  175. int return_valid; /* return code is valid */
  176. long return_code;/* syscall return code */
  177. int auditable; /* 1 if record should be written */
  178. int name_count;
  179. struct audit_names names[AUDIT_NAMES];
  180. char * filterkey; /* key for rule that triggered record */
  181. struct dentry * pwd;
  182. struct vfsmount * pwdmnt;
  183. struct audit_context *previous; /* For nested syscalls */
  184. struct audit_aux_data *aux;
  185. struct audit_aux_data *aux_pids;
  186. /* Save things to print about task_struct */
  187. pid_t pid, ppid;
  188. uid_t uid, euid, suid, fsuid;
  189. gid_t gid, egid, sgid, fsgid;
  190. unsigned long personality;
  191. int arch;
  192. pid_t target_pid;
  193. u32 target_sid;
  194. struct audit_tree_refs *trees, *first_trees;
  195. int tree_count;
  196. #if AUDIT_DEBUG
  197. int put_count;
  198. int ino_count;
  199. #endif
  200. };
  201. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  202. static inline int open_arg(int flags, int mask)
  203. {
  204. int n = ACC_MODE(flags);
  205. if (flags & (O_TRUNC | O_CREAT))
  206. n |= AUDIT_PERM_WRITE;
  207. return n & mask;
  208. }
  209. static int audit_match_perm(struct audit_context *ctx, int mask)
  210. {
  211. unsigned n = ctx->major;
  212. switch (audit_classify_syscall(ctx->arch, n)) {
  213. case 0: /* native */
  214. if ((mask & AUDIT_PERM_WRITE) &&
  215. audit_match_class(AUDIT_CLASS_WRITE, n))
  216. return 1;
  217. if ((mask & AUDIT_PERM_READ) &&
  218. audit_match_class(AUDIT_CLASS_READ, n))
  219. return 1;
  220. if ((mask & AUDIT_PERM_ATTR) &&
  221. audit_match_class(AUDIT_CLASS_CHATTR, n))
  222. return 1;
  223. return 0;
  224. case 1: /* 32bit on biarch */
  225. if ((mask & AUDIT_PERM_WRITE) &&
  226. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  227. return 1;
  228. if ((mask & AUDIT_PERM_READ) &&
  229. audit_match_class(AUDIT_CLASS_READ_32, n))
  230. return 1;
  231. if ((mask & AUDIT_PERM_ATTR) &&
  232. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  233. return 1;
  234. return 0;
  235. case 2: /* open */
  236. return mask & ACC_MODE(ctx->argv[1]);
  237. case 3: /* openat */
  238. return mask & ACC_MODE(ctx->argv[2]);
  239. case 4: /* socketcall */
  240. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  241. case 5: /* execve */
  242. return mask & AUDIT_PERM_EXEC;
  243. default:
  244. return 0;
  245. }
  246. }
  247. /*
  248. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  249. * ->first_trees points to its beginning, ->trees - to the current end of data.
  250. * ->tree_count is the number of free entries in array pointed to by ->trees.
  251. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  252. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  253. * it's going to remain 1-element for almost any setup) until we free context itself.
  254. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  255. */
  256. #ifdef CONFIG_AUDIT_TREE
  257. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  258. {
  259. struct audit_tree_refs *p = ctx->trees;
  260. int left = ctx->tree_count;
  261. if (likely(left)) {
  262. p->c[--left] = chunk;
  263. ctx->tree_count = left;
  264. return 1;
  265. }
  266. if (!p)
  267. return 0;
  268. p = p->next;
  269. if (p) {
  270. p->c[30] = chunk;
  271. ctx->trees = p;
  272. ctx->tree_count = 30;
  273. return 1;
  274. }
  275. return 0;
  276. }
  277. static int grow_tree_refs(struct audit_context *ctx)
  278. {
  279. struct audit_tree_refs *p = ctx->trees;
  280. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  281. if (!ctx->trees) {
  282. ctx->trees = p;
  283. return 0;
  284. }
  285. if (p)
  286. p->next = ctx->trees;
  287. else
  288. ctx->first_trees = ctx->trees;
  289. ctx->tree_count = 31;
  290. return 1;
  291. }
  292. #endif
  293. static void unroll_tree_refs(struct audit_context *ctx,
  294. struct audit_tree_refs *p, int count)
  295. {
  296. #ifdef CONFIG_AUDIT_TREE
  297. struct audit_tree_refs *q;
  298. int n;
  299. if (!p) {
  300. /* we started with empty chain */
  301. p = ctx->first_trees;
  302. count = 31;
  303. /* if the very first allocation has failed, nothing to do */
  304. if (!p)
  305. return;
  306. }
  307. n = count;
  308. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  309. while (n--) {
  310. audit_put_chunk(q->c[n]);
  311. q->c[n] = NULL;
  312. }
  313. }
  314. while (n-- > ctx->tree_count) {
  315. audit_put_chunk(q->c[n]);
  316. q->c[n] = NULL;
  317. }
  318. ctx->trees = p;
  319. ctx->tree_count = count;
  320. #endif
  321. }
  322. static void free_tree_refs(struct audit_context *ctx)
  323. {
  324. struct audit_tree_refs *p, *q;
  325. for (p = ctx->first_trees; p; p = q) {
  326. q = p->next;
  327. kfree(p);
  328. }
  329. }
  330. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  331. {
  332. #ifdef CONFIG_AUDIT_TREE
  333. struct audit_tree_refs *p;
  334. int n;
  335. if (!tree)
  336. return 0;
  337. /* full ones */
  338. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  339. for (n = 0; n < 31; n++)
  340. if (audit_tree_match(p->c[n], tree))
  341. return 1;
  342. }
  343. /* partial */
  344. if (p) {
  345. for (n = ctx->tree_count; n < 31; n++)
  346. if (audit_tree_match(p->c[n], tree))
  347. return 1;
  348. }
  349. #endif
  350. return 0;
  351. }
  352. /* Determine if any context name data matches a rule's watch data */
  353. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  354. * otherwise. */
  355. static int audit_filter_rules(struct task_struct *tsk,
  356. struct audit_krule *rule,
  357. struct audit_context *ctx,
  358. struct audit_names *name,
  359. enum audit_state *state)
  360. {
  361. int i, j, need_sid = 1;
  362. u32 sid;
  363. for (i = 0; i < rule->field_count; i++) {
  364. struct audit_field *f = &rule->fields[i];
  365. int result = 0;
  366. switch (f->type) {
  367. case AUDIT_PID:
  368. result = audit_comparator(tsk->pid, f->op, f->val);
  369. break;
  370. case AUDIT_PPID:
  371. if (ctx) {
  372. if (!ctx->ppid)
  373. ctx->ppid = sys_getppid();
  374. result = audit_comparator(ctx->ppid, f->op, f->val);
  375. }
  376. break;
  377. case AUDIT_UID:
  378. result = audit_comparator(tsk->uid, f->op, f->val);
  379. break;
  380. case AUDIT_EUID:
  381. result = audit_comparator(tsk->euid, f->op, f->val);
  382. break;
  383. case AUDIT_SUID:
  384. result = audit_comparator(tsk->suid, f->op, f->val);
  385. break;
  386. case AUDIT_FSUID:
  387. result = audit_comparator(tsk->fsuid, f->op, f->val);
  388. break;
  389. case AUDIT_GID:
  390. result = audit_comparator(tsk->gid, f->op, f->val);
  391. break;
  392. case AUDIT_EGID:
  393. result = audit_comparator(tsk->egid, f->op, f->val);
  394. break;
  395. case AUDIT_SGID:
  396. result = audit_comparator(tsk->sgid, f->op, f->val);
  397. break;
  398. case AUDIT_FSGID:
  399. result = audit_comparator(tsk->fsgid, f->op, f->val);
  400. break;
  401. case AUDIT_PERS:
  402. result = audit_comparator(tsk->personality, f->op, f->val);
  403. break;
  404. case AUDIT_ARCH:
  405. if (ctx)
  406. result = audit_comparator(ctx->arch, f->op, f->val);
  407. break;
  408. case AUDIT_EXIT:
  409. if (ctx && ctx->return_valid)
  410. result = audit_comparator(ctx->return_code, f->op, f->val);
  411. break;
  412. case AUDIT_SUCCESS:
  413. if (ctx && ctx->return_valid) {
  414. if (f->val)
  415. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  416. else
  417. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  418. }
  419. break;
  420. case AUDIT_DEVMAJOR:
  421. if (name)
  422. result = audit_comparator(MAJOR(name->dev),
  423. f->op, f->val);
  424. else if (ctx) {
  425. for (j = 0; j < ctx->name_count; j++) {
  426. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  427. ++result;
  428. break;
  429. }
  430. }
  431. }
  432. break;
  433. case AUDIT_DEVMINOR:
  434. if (name)
  435. result = audit_comparator(MINOR(name->dev),
  436. f->op, f->val);
  437. else if (ctx) {
  438. for (j = 0; j < ctx->name_count; j++) {
  439. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  440. ++result;
  441. break;
  442. }
  443. }
  444. }
  445. break;
  446. case AUDIT_INODE:
  447. if (name)
  448. result = (name->ino == f->val);
  449. else if (ctx) {
  450. for (j = 0; j < ctx->name_count; j++) {
  451. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  452. ++result;
  453. break;
  454. }
  455. }
  456. }
  457. break;
  458. case AUDIT_WATCH:
  459. if (name && rule->watch->ino != (unsigned long)-1)
  460. result = (name->dev == rule->watch->dev &&
  461. name->ino == rule->watch->ino);
  462. break;
  463. case AUDIT_DIR:
  464. if (ctx)
  465. result = match_tree_refs(ctx, rule->tree);
  466. break;
  467. case AUDIT_LOGINUID:
  468. result = 0;
  469. if (ctx)
  470. result = audit_comparator(ctx->loginuid, f->op, f->val);
  471. break;
  472. case AUDIT_SUBJ_USER:
  473. case AUDIT_SUBJ_ROLE:
  474. case AUDIT_SUBJ_TYPE:
  475. case AUDIT_SUBJ_SEN:
  476. case AUDIT_SUBJ_CLR:
  477. /* NOTE: this may return negative values indicating
  478. a temporary error. We simply treat this as a
  479. match for now to avoid losing information that
  480. may be wanted. An error message will also be
  481. logged upon error */
  482. if (f->se_rule) {
  483. if (need_sid) {
  484. selinux_get_task_sid(tsk, &sid);
  485. need_sid = 0;
  486. }
  487. result = selinux_audit_rule_match(sid, f->type,
  488. f->op,
  489. f->se_rule,
  490. ctx);
  491. }
  492. break;
  493. case AUDIT_OBJ_USER:
  494. case AUDIT_OBJ_ROLE:
  495. case AUDIT_OBJ_TYPE:
  496. case AUDIT_OBJ_LEV_LOW:
  497. case AUDIT_OBJ_LEV_HIGH:
  498. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  499. also applies here */
  500. if (f->se_rule) {
  501. /* Find files that match */
  502. if (name) {
  503. result = selinux_audit_rule_match(
  504. name->osid, f->type, f->op,
  505. f->se_rule, ctx);
  506. } else if (ctx) {
  507. for (j = 0; j < ctx->name_count; j++) {
  508. if (selinux_audit_rule_match(
  509. ctx->names[j].osid,
  510. f->type, f->op,
  511. f->se_rule, ctx)) {
  512. ++result;
  513. break;
  514. }
  515. }
  516. }
  517. /* Find ipc objects that match */
  518. if (ctx) {
  519. struct audit_aux_data *aux;
  520. for (aux = ctx->aux; aux;
  521. aux = aux->next) {
  522. if (aux->type == AUDIT_IPC) {
  523. struct audit_aux_data_ipcctl *axi = (void *)aux;
  524. if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
  525. ++result;
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. }
  532. break;
  533. case AUDIT_ARG0:
  534. case AUDIT_ARG1:
  535. case AUDIT_ARG2:
  536. case AUDIT_ARG3:
  537. if (ctx)
  538. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  539. break;
  540. case AUDIT_FILTERKEY:
  541. /* ignore this field for filtering */
  542. result = 1;
  543. break;
  544. case AUDIT_PERM:
  545. result = audit_match_perm(ctx, f->val);
  546. break;
  547. }
  548. if (!result)
  549. return 0;
  550. }
  551. if (rule->filterkey)
  552. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  553. switch (rule->action) {
  554. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  555. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  556. }
  557. return 1;
  558. }
  559. /* At process creation time, we can determine if system-call auditing is
  560. * completely disabled for this task. Since we only have the task
  561. * structure at this point, we can only check uid and gid.
  562. */
  563. static enum audit_state audit_filter_task(struct task_struct *tsk)
  564. {
  565. struct audit_entry *e;
  566. enum audit_state state;
  567. rcu_read_lock();
  568. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  569. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  570. rcu_read_unlock();
  571. return state;
  572. }
  573. }
  574. rcu_read_unlock();
  575. return AUDIT_BUILD_CONTEXT;
  576. }
  577. /* At syscall entry and exit time, this filter is called if the
  578. * audit_state is not low enough that auditing cannot take place, but is
  579. * also not high enough that we already know we have to write an audit
  580. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  581. */
  582. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  583. struct audit_context *ctx,
  584. struct list_head *list)
  585. {
  586. struct audit_entry *e;
  587. enum audit_state state;
  588. if (audit_pid && tsk->tgid == audit_pid)
  589. return AUDIT_DISABLED;
  590. rcu_read_lock();
  591. if (!list_empty(list)) {
  592. int word = AUDIT_WORD(ctx->major);
  593. int bit = AUDIT_BIT(ctx->major);
  594. list_for_each_entry_rcu(e, list, list) {
  595. if ((e->rule.mask[word] & bit) == bit &&
  596. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  597. &state)) {
  598. rcu_read_unlock();
  599. return state;
  600. }
  601. }
  602. }
  603. rcu_read_unlock();
  604. return AUDIT_BUILD_CONTEXT;
  605. }
  606. /* At syscall exit time, this filter is called if any audit_names[] have been
  607. * collected during syscall processing. We only check rules in sublists at hash
  608. * buckets applicable to the inode numbers in audit_names[].
  609. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  610. */
  611. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  612. struct audit_context *ctx)
  613. {
  614. int i;
  615. struct audit_entry *e;
  616. enum audit_state state;
  617. if (audit_pid && tsk->tgid == audit_pid)
  618. return AUDIT_DISABLED;
  619. rcu_read_lock();
  620. for (i = 0; i < ctx->name_count; i++) {
  621. int word = AUDIT_WORD(ctx->major);
  622. int bit = AUDIT_BIT(ctx->major);
  623. struct audit_names *n = &ctx->names[i];
  624. int h = audit_hash_ino((u32)n->ino);
  625. struct list_head *list = &audit_inode_hash[h];
  626. if (list_empty(list))
  627. continue;
  628. list_for_each_entry_rcu(e, list, list) {
  629. if ((e->rule.mask[word] & bit) == bit &&
  630. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  631. rcu_read_unlock();
  632. return state;
  633. }
  634. }
  635. }
  636. rcu_read_unlock();
  637. return AUDIT_BUILD_CONTEXT;
  638. }
  639. void audit_set_auditable(struct audit_context *ctx)
  640. {
  641. ctx->auditable = 1;
  642. }
  643. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  644. int return_valid,
  645. int return_code)
  646. {
  647. struct audit_context *context = tsk->audit_context;
  648. if (likely(!context))
  649. return NULL;
  650. context->return_valid = return_valid;
  651. context->return_code = return_code;
  652. if (context->in_syscall && !context->dummy && !context->auditable) {
  653. enum audit_state state;
  654. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  655. if (state == AUDIT_RECORD_CONTEXT) {
  656. context->auditable = 1;
  657. goto get_context;
  658. }
  659. state = audit_filter_inodes(tsk, context);
  660. if (state == AUDIT_RECORD_CONTEXT)
  661. context->auditable = 1;
  662. }
  663. get_context:
  664. tsk->audit_context = NULL;
  665. return context;
  666. }
  667. static inline void audit_free_names(struct audit_context *context)
  668. {
  669. int i;
  670. #if AUDIT_DEBUG == 2
  671. if (context->auditable
  672. ||context->put_count + context->ino_count != context->name_count) {
  673. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  674. " name_count=%d put_count=%d"
  675. " ino_count=%d [NOT freeing]\n",
  676. __FILE__, __LINE__,
  677. context->serial, context->major, context->in_syscall,
  678. context->name_count, context->put_count,
  679. context->ino_count);
  680. for (i = 0; i < context->name_count; i++) {
  681. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  682. context->names[i].name,
  683. context->names[i].name ?: "(null)");
  684. }
  685. dump_stack();
  686. return;
  687. }
  688. #endif
  689. #if AUDIT_DEBUG
  690. context->put_count = 0;
  691. context->ino_count = 0;
  692. #endif
  693. for (i = 0; i < context->name_count; i++) {
  694. if (context->names[i].name && context->names[i].name_put)
  695. __putname(context->names[i].name);
  696. }
  697. context->name_count = 0;
  698. if (context->pwd)
  699. dput(context->pwd);
  700. if (context->pwdmnt)
  701. mntput(context->pwdmnt);
  702. context->pwd = NULL;
  703. context->pwdmnt = NULL;
  704. }
  705. static inline void audit_free_aux(struct audit_context *context)
  706. {
  707. struct audit_aux_data *aux;
  708. while ((aux = context->aux)) {
  709. context->aux = aux->next;
  710. kfree(aux);
  711. }
  712. while ((aux = context->aux_pids)) {
  713. context->aux_pids = aux->next;
  714. kfree(aux);
  715. }
  716. }
  717. static inline void audit_zero_context(struct audit_context *context,
  718. enum audit_state state)
  719. {
  720. uid_t loginuid = context->loginuid;
  721. memset(context, 0, sizeof(*context));
  722. context->state = state;
  723. context->loginuid = loginuid;
  724. }
  725. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  726. {
  727. struct audit_context *context;
  728. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  729. return NULL;
  730. audit_zero_context(context, state);
  731. return context;
  732. }
  733. /**
  734. * audit_alloc - allocate an audit context block for a task
  735. * @tsk: task
  736. *
  737. * Filter on the task information and allocate a per-task audit context
  738. * if necessary. Doing so turns on system call auditing for the
  739. * specified task. This is called from copy_process, so no lock is
  740. * needed.
  741. */
  742. int audit_alloc(struct task_struct *tsk)
  743. {
  744. struct audit_context *context;
  745. enum audit_state state;
  746. if (likely(!audit_enabled))
  747. return 0; /* Return if not auditing. */
  748. state = audit_filter_task(tsk);
  749. if (likely(state == AUDIT_DISABLED))
  750. return 0;
  751. if (!(context = audit_alloc_context(state))) {
  752. audit_log_lost("out of memory in audit_alloc");
  753. return -ENOMEM;
  754. }
  755. /* Preserve login uid */
  756. context->loginuid = -1;
  757. if (current->audit_context)
  758. context->loginuid = current->audit_context->loginuid;
  759. tsk->audit_context = context;
  760. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  761. return 0;
  762. }
  763. static inline void audit_free_context(struct audit_context *context)
  764. {
  765. struct audit_context *previous;
  766. int count = 0;
  767. do {
  768. previous = context->previous;
  769. if (previous || (count && count < 10)) {
  770. ++count;
  771. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  772. " freeing multiple contexts (%d)\n",
  773. context->serial, context->major,
  774. context->name_count, count);
  775. }
  776. audit_free_names(context);
  777. unroll_tree_refs(context, NULL, 0);
  778. free_tree_refs(context);
  779. audit_free_aux(context);
  780. kfree(context->filterkey);
  781. kfree(context);
  782. context = previous;
  783. } while (context);
  784. if (count >= 10)
  785. printk(KERN_ERR "audit: freed %d contexts\n", count);
  786. }
  787. void audit_log_task_context(struct audit_buffer *ab)
  788. {
  789. char *ctx = NULL;
  790. unsigned len;
  791. int error;
  792. u32 sid;
  793. selinux_get_task_sid(current, &sid);
  794. if (!sid)
  795. return;
  796. error = selinux_sid_to_string(sid, &ctx, &len);
  797. if (error) {
  798. if (error != -EINVAL)
  799. goto error_path;
  800. return;
  801. }
  802. audit_log_format(ab, " subj=%s", ctx);
  803. kfree(ctx);
  804. return;
  805. error_path:
  806. audit_panic("error in audit_log_task_context");
  807. return;
  808. }
  809. EXPORT_SYMBOL(audit_log_task_context);
  810. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  811. {
  812. char name[sizeof(tsk->comm)];
  813. struct mm_struct *mm = tsk->mm;
  814. struct vm_area_struct *vma;
  815. /* tsk == current */
  816. get_task_comm(name, tsk);
  817. audit_log_format(ab, " comm=");
  818. audit_log_untrustedstring(ab, name);
  819. if (mm) {
  820. down_read(&mm->mmap_sem);
  821. vma = mm->mmap;
  822. while (vma) {
  823. if ((vma->vm_flags & VM_EXECUTABLE) &&
  824. vma->vm_file) {
  825. audit_log_d_path(ab, "exe=",
  826. vma->vm_file->f_path.dentry,
  827. vma->vm_file->f_path.mnt);
  828. break;
  829. }
  830. vma = vma->vm_next;
  831. }
  832. up_read(&mm->mmap_sem);
  833. }
  834. audit_log_task_context(ab);
  835. }
  836. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  837. u32 sid)
  838. {
  839. struct audit_buffer *ab;
  840. char *s = NULL;
  841. u32 len;
  842. int rc = 0;
  843. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  844. if (!ab)
  845. return 1;
  846. if (selinux_sid_to_string(sid, &s, &len)) {
  847. audit_log_format(ab, "opid=%d obj=(none)", pid);
  848. rc = 1;
  849. } else
  850. audit_log_format(ab, "opid=%d obj=%s", pid, s);
  851. audit_log_end(ab);
  852. kfree(s);
  853. return rc;
  854. }
  855. static void audit_log_execve_info(struct audit_buffer *ab,
  856. struct audit_aux_data_execve *axi)
  857. {
  858. int i;
  859. long len, ret;
  860. const char __user *p;
  861. char *buf;
  862. if (axi->mm != current->mm)
  863. return; /* execve failed, no additional info */
  864. p = (const char __user *)axi->mm->arg_start;
  865. for (i = 0; i < axi->argc; i++, p += len) {
  866. len = strnlen_user(p, MAX_ARG_STRLEN);
  867. /*
  868. * We just created this mm, if we can't find the strings
  869. * we just copied into it something is _very_ wrong. Similar
  870. * for strings that are too long, we should not have created
  871. * any.
  872. */
  873. if (!len || len > MAX_ARG_STRLEN) {
  874. WARN_ON(1);
  875. send_sig(SIGKILL, current, 0);
  876. }
  877. buf = kmalloc(len, GFP_KERNEL);
  878. if (!buf) {
  879. audit_panic("out of memory for argv string\n");
  880. break;
  881. }
  882. ret = copy_from_user(buf, p, len);
  883. /*
  884. * There is no reason for this copy to be short. We just
  885. * copied them here, and the mm hasn't been exposed to user-
  886. * space yet.
  887. */
  888. if (ret) {
  889. WARN_ON(1);
  890. send_sig(SIGKILL, current, 0);
  891. }
  892. audit_log_format(ab, "a%d=", i);
  893. audit_log_untrustedstring(ab, buf);
  894. audit_log_format(ab, "\n");
  895. kfree(buf);
  896. }
  897. }
  898. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  899. {
  900. int i, call_panic = 0;
  901. struct audit_buffer *ab;
  902. struct audit_aux_data *aux;
  903. const char *tty;
  904. /* tsk == current */
  905. context->pid = tsk->pid;
  906. if (!context->ppid)
  907. context->ppid = sys_getppid();
  908. context->uid = tsk->uid;
  909. context->gid = tsk->gid;
  910. context->euid = tsk->euid;
  911. context->suid = tsk->suid;
  912. context->fsuid = tsk->fsuid;
  913. context->egid = tsk->egid;
  914. context->sgid = tsk->sgid;
  915. context->fsgid = tsk->fsgid;
  916. context->personality = tsk->personality;
  917. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  918. if (!ab)
  919. return; /* audit_panic has been called */
  920. audit_log_format(ab, "arch=%x syscall=%d",
  921. context->arch, context->major);
  922. if (context->personality != PER_LINUX)
  923. audit_log_format(ab, " per=%lx", context->personality);
  924. if (context->return_valid)
  925. audit_log_format(ab, " success=%s exit=%ld",
  926. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  927. context->return_code);
  928. mutex_lock(&tty_mutex);
  929. read_lock(&tasklist_lock);
  930. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  931. tty = tsk->signal->tty->name;
  932. else
  933. tty = "(none)";
  934. read_unlock(&tasklist_lock);
  935. audit_log_format(ab,
  936. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  937. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  938. " euid=%u suid=%u fsuid=%u"
  939. " egid=%u sgid=%u fsgid=%u tty=%s",
  940. context->argv[0],
  941. context->argv[1],
  942. context->argv[2],
  943. context->argv[3],
  944. context->name_count,
  945. context->ppid,
  946. context->pid,
  947. context->loginuid,
  948. context->uid,
  949. context->gid,
  950. context->euid, context->suid, context->fsuid,
  951. context->egid, context->sgid, context->fsgid, tty);
  952. mutex_unlock(&tty_mutex);
  953. audit_log_task_info(ab, tsk);
  954. if (context->filterkey) {
  955. audit_log_format(ab, " key=");
  956. audit_log_untrustedstring(ab, context->filterkey);
  957. } else
  958. audit_log_format(ab, " key=(null)");
  959. audit_log_end(ab);
  960. for (aux = context->aux; aux; aux = aux->next) {
  961. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  962. if (!ab)
  963. continue; /* audit_panic has been called */
  964. switch (aux->type) {
  965. case AUDIT_MQ_OPEN: {
  966. struct audit_aux_data_mq_open *axi = (void *)aux;
  967. audit_log_format(ab,
  968. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  969. "mq_msgsize=%ld mq_curmsgs=%ld",
  970. axi->oflag, axi->mode, axi->attr.mq_flags,
  971. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  972. axi->attr.mq_curmsgs);
  973. break; }
  974. case AUDIT_MQ_SENDRECV: {
  975. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  976. audit_log_format(ab,
  977. "mqdes=%d msg_len=%zd msg_prio=%u "
  978. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  979. axi->mqdes, axi->msg_len, axi->msg_prio,
  980. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  981. break; }
  982. case AUDIT_MQ_NOTIFY: {
  983. struct audit_aux_data_mq_notify *axi = (void *)aux;
  984. audit_log_format(ab,
  985. "mqdes=%d sigev_signo=%d",
  986. axi->mqdes,
  987. axi->notification.sigev_signo);
  988. break; }
  989. case AUDIT_MQ_GETSETATTR: {
  990. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  991. audit_log_format(ab,
  992. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  993. "mq_curmsgs=%ld ",
  994. axi->mqdes,
  995. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  996. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  997. break; }
  998. case AUDIT_IPC: {
  999. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1000. audit_log_format(ab,
  1001. "ouid=%u ogid=%u mode=%#o",
  1002. axi->uid, axi->gid, axi->mode);
  1003. if (axi->osid != 0) {
  1004. char *ctx = NULL;
  1005. u32 len;
  1006. if (selinux_sid_to_string(
  1007. axi->osid, &ctx, &len)) {
  1008. audit_log_format(ab, " osid=%u",
  1009. axi->osid);
  1010. call_panic = 1;
  1011. } else
  1012. audit_log_format(ab, " obj=%s", ctx);
  1013. kfree(ctx);
  1014. }
  1015. break; }
  1016. case AUDIT_IPC_SET_PERM: {
  1017. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1018. audit_log_format(ab,
  1019. "qbytes=%lx ouid=%u ogid=%u mode=%#o",
  1020. axi->qbytes, axi->uid, axi->gid, axi->mode);
  1021. break; }
  1022. case AUDIT_EXECVE: {
  1023. struct audit_aux_data_execve *axi = (void *)aux;
  1024. audit_log_execve_info(ab, axi);
  1025. break; }
  1026. case AUDIT_SOCKETCALL: {
  1027. int i;
  1028. struct audit_aux_data_socketcall *axs = (void *)aux;
  1029. audit_log_format(ab, "nargs=%d", axs->nargs);
  1030. for (i=0; i<axs->nargs; i++)
  1031. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  1032. break; }
  1033. case AUDIT_SOCKADDR: {
  1034. struct audit_aux_data_sockaddr *axs = (void *)aux;
  1035. audit_log_format(ab, "saddr=");
  1036. audit_log_hex(ab, axs->a, axs->len);
  1037. break; }
  1038. case AUDIT_FD_PAIR: {
  1039. struct audit_aux_data_fd_pair *axs = (void *)aux;
  1040. audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
  1041. break; }
  1042. }
  1043. audit_log_end(ab);
  1044. }
  1045. for (aux = context->aux_pids; aux; aux = aux->next) {
  1046. struct audit_aux_data_pids *axs = (void *)aux;
  1047. int i;
  1048. for (i = 0; i < axs->pid_count; i++)
  1049. if (audit_log_pid_context(context, axs->target_pid[i],
  1050. axs->target_sid[i]))
  1051. call_panic = 1;
  1052. }
  1053. if (context->target_pid &&
  1054. audit_log_pid_context(context, context->target_pid,
  1055. context->target_sid))
  1056. call_panic = 1;
  1057. if (context->pwd && context->pwdmnt) {
  1058. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1059. if (ab) {
  1060. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  1061. audit_log_end(ab);
  1062. }
  1063. }
  1064. for (i = 0; i < context->name_count; i++) {
  1065. struct audit_names *n = &context->names[i];
  1066. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1067. if (!ab)
  1068. continue; /* audit_panic has been called */
  1069. audit_log_format(ab, "item=%d", i);
  1070. if (n->name) {
  1071. switch(n->name_len) {
  1072. case AUDIT_NAME_FULL:
  1073. /* log the full path */
  1074. audit_log_format(ab, " name=");
  1075. audit_log_untrustedstring(ab, n->name);
  1076. break;
  1077. case 0:
  1078. /* name was specified as a relative path and the
  1079. * directory component is the cwd */
  1080. audit_log_d_path(ab, " name=", context->pwd,
  1081. context->pwdmnt);
  1082. break;
  1083. default:
  1084. /* log the name's directory component */
  1085. audit_log_format(ab, " name=");
  1086. audit_log_n_untrustedstring(ab, n->name_len,
  1087. n->name);
  1088. }
  1089. } else
  1090. audit_log_format(ab, " name=(null)");
  1091. if (n->ino != (unsigned long)-1) {
  1092. audit_log_format(ab, " inode=%lu"
  1093. " dev=%02x:%02x mode=%#o"
  1094. " ouid=%u ogid=%u rdev=%02x:%02x",
  1095. n->ino,
  1096. MAJOR(n->dev),
  1097. MINOR(n->dev),
  1098. n->mode,
  1099. n->uid,
  1100. n->gid,
  1101. MAJOR(n->rdev),
  1102. MINOR(n->rdev));
  1103. }
  1104. if (n->osid != 0) {
  1105. char *ctx = NULL;
  1106. u32 len;
  1107. if (selinux_sid_to_string(
  1108. n->osid, &ctx, &len)) {
  1109. audit_log_format(ab, " osid=%u", n->osid);
  1110. call_panic = 2;
  1111. } else
  1112. audit_log_format(ab, " obj=%s", ctx);
  1113. kfree(ctx);
  1114. }
  1115. audit_log_end(ab);
  1116. }
  1117. if (call_panic)
  1118. audit_panic("error converting sid to string");
  1119. }
  1120. /**
  1121. * audit_free - free a per-task audit context
  1122. * @tsk: task whose audit context block to free
  1123. *
  1124. * Called from copy_process and do_exit
  1125. */
  1126. void audit_free(struct task_struct *tsk)
  1127. {
  1128. struct audit_context *context;
  1129. context = audit_get_context(tsk, 0, 0);
  1130. if (likely(!context))
  1131. return;
  1132. /* Check for system calls that do not go through the exit
  1133. * function (e.g., exit_group), then free context block.
  1134. * We use GFP_ATOMIC here because we might be doing this
  1135. * in the context of the idle thread */
  1136. /* that can happen only if we are called from do_exit() */
  1137. if (context->in_syscall && context->auditable)
  1138. audit_log_exit(context, tsk);
  1139. audit_free_context(context);
  1140. }
  1141. /**
  1142. * audit_syscall_entry - fill in an audit record at syscall entry
  1143. * @tsk: task being audited
  1144. * @arch: architecture type
  1145. * @major: major syscall type (function)
  1146. * @a1: additional syscall register 1
  1147. * @a2: additional syscall register 2
  1148. * @a3: additional syscall register 3
  1149. * @a4: additional syscall register 4
  1150. *
  1151. * Fill in audit context at syscall entry. This only happens if the
  1152. * audit context was created when the task was created and the state or
  1153. * filters demand the audit context be built. If the state from the
  1154. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1155. * then the record will be written at syscall exit time (otherwise, it
  1156. * will only be written if another part of the kernel requests that it
  1157. * be written).
  1158. */
  1159. void audit_syscall_entry(int arch, int major,
  1160. unsigned long a1, unsigned long a2,
  1161. unsigned long a3, unsigned long a4)
  1162. {
  1163. struct task_struct *tsk = current;
  1164. struct audit_context *context = tsk->audit_context;
  1165. enum audit_state state;
  1166. BUG_ON(!context);
  1167. /*
  1168. * This happens only on certain architectures that make system
  1169. * calls in kernel_thread via the entry.S interface, instead of
  1170. * with direct calls. (If you are porting to a new
  1171. * architecture, hitting this condition can indicate that you
  1172. * got the _exit/_leave calls backward in entry.S.)
  1173. *
  1174. * i386 no
  1175. * x86_64 no
  1176. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  1177. *
  1178. * This also happens with vm86 emulation in a non-nested manner
  1179. * (entries without exits), so this case must be caught.
  1180. */
  1181. if (context->in_syscall) {
  1182. struct audit_context *newctx;
  1183. #if AUDIT_DEBUG
  1184. printk(KERN_ERR
  1185. "audit(:%d) pid=%d in syscall=%d;"
  1186. " entering syscall=%d\n",
  1187. context->serial, tsk->pid, context->major, major);
  1188. #endif
  1189. newctx = audit_alloc_context(context->state);
  1190. if (newctx) {
  1191. newctx->previous = context;
  1192. context = newctx;
  1193. tsk->audit_context = newctx;
  1194. } else {
  1195. /* If we can't alloc a new context, the best we
  1196. * can do is to leak memory (any pending putname
  1197. * will be lost). The only other alternative is
  1198. * to abandon auditing. */
  1199. audit_zero_context(context, context->state);
  1200. }
  1201. }
  1202. BUG_ON(context->in_syscall || context->name_count);
  1203. if (!audit_enabled)
  1204. return;
  1205. context->arch = arch;
  1206. context->major = major;
  1207. context->argv[0] = a1;
  1208. context->argv[1] = a2;
  1209. context->argv[2] = a3;
  1210. context->argv[3] = a4;
  1211. state = context->state;
  1212. context->dummy = !audit_n_rules;
  1213. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1214. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1215. if (likely(state == AUDIT_DISABLED))
  1216. return;
  1217. context->serial = 0;
  1218. context->ctime = CURRENT_TIME;
  1219. context->in_syscall = 1;
  1220. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1221. context->ppid = 0;
  1222. }
  1223. /**
  1224. * audit_syscall_exit - deallocate audit context after a system call
  1225. * @tsk: task being audited
  1226. * @valid: success/failure flag
  1227. * @return_code: syscall return value
  1228. *
  1229. * Tear down after system call. If the audit context has been marked as
  1230. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1231. * filtering, or because some other part of the kernel write an audit
  1232. * message), then write out the syscall information. In call cases,
  1233. * free the names stored from getname().
  1234. */
  1235. void audit_syscall_exit(int valid, long return_code)
  1236. {
  1237. struct task_struct *tsk = current;
  1238. struct audit_context *context;
  1239. context = audit_get_context(tsk, valid, return_code);
  1240. if (likely(!context))
  1241. return;
  1242. if (context->in_syscall && context->auditable)
  1243. audit_log_exit(context, tsk);
  1244. context->in_syscall = 0;
  1245. context->auditable = 0;
  1246. if (context->previous) {
  1247. struct audit_context *new_context = context->previous;
  1248. context->previous = NULL;
  1249. audit_free_context(context);
  1250. tsk->audit_context = new_context;
  1251. } else {
  1252. audit_free_names(context);
  1253. unroll_tree_refs(context, NULL, 0);
  1254. audit_free_aux(context);
  1255. context->aux = NULL;
  1256. context->aux_pids = NULL;
  1257. context->target_pid = 0;
  1258. context->target_sid = 0;
  1259. kfree(context->filterkey);
  1260. context->filterkey = NULL;
  1261. tsk->audit_context = context;
  1262. }
  1263. }
  1264. static inline void handle_one(const struct inode *inode)
  1265. {
  1266. #ifdef CONFIG_AUDIT_TREE
  1267. struct audit_context *context;
  1268. struct audit_tree_refs *p;
  1269. struct audit_chunk *chunk;
  1270. int count;
  1271. if (likely(list_empty(&inode->inotify_watches)))
  1272. return;
  1273. context = current->audit_context;
  1274. p = context->trees;
  1275. count = context->tree_count;
  1276. rcu_read_lock();
  1277. chunk = audit_tree_lookup(inode);
  1278. rcu_read_unlock();
  1279. if (!chunk)
  1280. return;
  1281. if (likely(put_tree_ref(context, chunk)))
  1282. return;
  1283. if (unlikely(!grow_tree_refs(context))) {
  1284. printk(KERN_WARNING "out of memory, audit has lost a tree reference");
  1285. audit_set_auditable(context);
  1286. audit_put_chunk(chunk);
  1287. unroll_tree_refs(context, p, count);
  1288. return;
  1289. }
  1290. put_tree_ref(context, chunk);
  1291. #endif
  1292. }
  1293. static void handle_path(const struct dentry *dentry)
  1294. {
  1295. #ifdef CONFIG_AUDIT_TREE
  1296. struct audit_context *context;
  1297. struct audit_tree_refs *p;
  1298. const struct dentry *d, *parent;
  1299. struct audit_chunk *drop;
  1300. unsigned long seq;
  1301. int count;
  1302. context = current->audit_context;
  1303. p = context->trees;
  1304. count = context->tree_count;
  1305. retry:
  1306. drop = NULL;
  1307. d = dentry;
  1308. rcu_read_lock();
  1309. seq = read_seqbegin(&rename_lock);
  1310. for(;;) {
  1311. struct inode *inode = d->d_inode;
  1312. if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
  1313. struct audit_chunk *chunk;
  1314. chunk = audit_tree_lookup(inode);
  1315. if (chunk) {
  1316. if (unlikely(!put_tree_ref(context, chunk))) {
  1317. drop = chunk;
  1318. break;
  1319. }
  1320. }
  1321. }
  1322. parent = d->d_parent;
  1323. if (parent == d)
  1324. break;
  1325. d = parent;
  1326. }
  1327. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1328. rcu_read_unlock();
  1329. if (!drop) {
  1330. /* just a race with rename */
  1331. unroll_tree_refs(context, p, count);
  1332. goto retry;
  1333. }
  1334. audit_put_chunk(drop);
  1335. if (grow_tree_refs(context)) {
  1336. /* OK, got more space */
  1337. unroll_tree_refs(context, p, count);
  1338. goto retry;
  1339. }
  1340. /* too bad */
  1341. printk(KERN_WARNING
  1342. "out of memory, audit has lost a tree reference");
  1343. unroll_tree_refs(context, p, count);
  1344. audit_set_auditable(context);
  1345. return;
  1346. }
  1347. rcu_read_unlock();
  1348. #endif
  1349. }
  1350. /**
  1351. * audit_getname - add a name to the list
  1352. * @name: name to add
  1353. *
  1354. * Add a name to the list of audit names for this context.
  1355. * Called from fs/namei.c:getname().
  1356. */
  1357. void __audit_getname(const char *name)
  1358. {
  1359. struct audit_context *context = current->audit_context;
  1360. if (IS_ERR(name) || !name)
  1361. return;
  1362. if (!context->in_syscall) {
  1363. #if AUDIT_DEBUG == 2
  1364. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1365. __FILE__, __LINE__, context->serial, name);
  1366. dump_stack();
  1367. #endif
  1368. return;
  1369. }
  1370. BUG_ON(context->name_count >= AUDIT_NAMES);
  1371. context->names[context->name_count].name = name;
  1372. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1373. context->names[context->name_count].name_put = 1;
  1374. context->names[context->name_count].ino = (unsigned long)-1;
  1375. context->names[context->name_count].osid = 0;
  1376. ++context->name_count;
  1377. if (!context->pwd) {
  1378. read_lock(&current->fs->lock);
  1379. context->pwd = dget(current->fs->pwd);
  1380. context->pwdmnt = mntget(current->fs->pwdmnt);
  1381. read_unlock(&current->fs->lock);
  1382. }
  1383. }
  1384. /* audit_putname - intercept a putname request
  1385. * @name: name to intercept and delay for putname
  1386. *
  1387. * If we have stored the name from getname in the audit context,
  1388. * then we delay the putname until syscall exit.
  1389. * Called from include/linux/fs.h:putname().
  1390. */
  1391. void audit_putname(const char *name)
  1392. {
  1393. struct audit_context *context = current->audit_context;
  1394. BUG_ON(!context);
  1395. if (!context->in_syscall) {
  1396. #if AUDIT_DEBUG == 2
  1397. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1398. __FILE__, __LINE__, context->serial, name);
  1399. if (context->name_count) {
  1400. int i;
  1401. for (i = 0; i < context->name_count; i++)
  1402. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1403. context->names[i].name,
  1404. context->names[i].name ?: "(null)");
  1405. }
  1406. #endif
  1407. __putname(name);
  1408. }
  1409. #if AUDIT_DEBUG
  1410. else {
  1411. ++context->put_count;
  1412. if (context->put_count > context->name_count) {
  1413. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1414. " in_syscall=%d putname(%p) name_count=%d"
  1415. " put_count=%d\n",
  1416. __FILE__, __LINE__,
  1417. context->serial, context->major,
  1418. context->in_syscall, name, context->name_count,
  1419. context->put_count);
  1420. dump_stack();
  1421. }
  1422. }
  1423. #endif
  1424. }
  1425. static int audit_inc_name_count(struct audit_context *context,
  1426. const struct inode *inode)
  1427. {
  1428. if (context->name_count >= AUDIT_NAMES) {
  1429. if (inode)
  1430. printk(KERN_DEBUG "name_count maxed, losing inode data: "
  1431. "dev=%02x:%02x, inode=%lu",
  1432. MAJOR(inode->i_sb->s_dev),
  1433. MINOR(inode->i_sb->s_dev),
  1434. inode->i_ino);
  1435. else
  1436. printk(KERN_DEBUG "name_count maxed, losing inode data");
  1437. return 1;
  1438. }
  1439. context->name_count++;
  1440. #if AUDIT_DEBUG
  1441. context->ino_count++;
  1442. #endif
  1443. return 0;
  1444. }
  1445. /* Copy inode data into an audit_names. */
  1446. static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
  1447. {
  1448. name->ino = inode->i_ino;
  1449. name->dev = inode->i_sb->s_dev;
  1450. name->mode = inode->i_mode;
  1451. name->uid = inode->i_uid;
  1452. name->gid = inode->i_gid;
  1453. name->rdev = inode->i_rdev;
  1454. selinux_get_inode_sid(inode, &name->osid);
  1455. }
  1456. /**
  1457. * audit_inode - store the inode and device from a lookup
  1458. * @name: name being audited
  1459. * @dentry: dentry being audited
  1460. *
  1461. * Called from fs/namei.c:path_lookup().
  1462. */
  1463. void __audit_inode(const char *name, const struct dentry *dentry)
  1464. {
  1465. int idx;
  1466. struct audit_context *context = current->audit_context;
  1467. const struct inode *inode = dentry->d_inode;
  1468. if (!context->in_syscall)
  1469. return;
  1470. if (context->name_count
  1471. && context->names[context->name_count-1].name
  1472. && context->names[context->name_count-1].name == name)
  1473. idx = context->name_count - 1;
  1474. else if (context->name_count > 1
  1475. && context->names[context->name_count-2].name
  1476. && context->names[context->name_count-2].name == name)
  1477. idx = context->name_count - 2;
  1478. else {
  1479. /* FIXME: how much do we care about inodes that have no
  1480. * associated name? */
  1481. if (audit_inc_name_count(context, inode))
  1482. return;
  1483. idx = context->name_count - 1;
  1484. context->names[idx].name = NULL;
  1485. }
  1486. handle_path(dentry);
  1487. audit_copy_inode(&context->names[idx], inode);
  1488. }
  1489. /**
  1490. * audit_inode_child - collect inode info for created/removed objects
  1491. * @dname: inode's dentry name
  1492. * @dentry: dentry being audited
  1493. * @parent: inode of dentry parent
  1494. *
  1495. * For syscalls that create or remove filesystem objects, audit_inode
  1496. * can only collect information for the filesystem object's parent.
  1497. * This call updates the audit context with the child's information.
  1498. * Syscalls that create a new filesystem object must be hooked after
  1499. * the object is created. Syscalls that remove a filesystem object
  1500. * must be hooked prior, in order to capture the target inode during
  1501. * unsuccessful attempts.
  1502. */
  1503. void __audit_inode_child(const char *dname, const struct dentry *dentry,
  1504. const struct inode *parent)
  1505. {
  1506. int idx;
  1507. struct audit_context *context = current->audit_context;
  1508. const char *found_parent = NULL, *found_child = NULL;
  1509. const struct inode *inode = dentry->d_inode;
  1510. int dirlen = 0;
  1511. if (!context->in_syscall)
  1512. return;
  1513. if (inode)
  1514. handle_one(inode);
  1515. /* determine matching parent */
  1516. if (!dname)
  1517. goto add_names;
  1518. /* parent is more likely, look for it first */
  1519. for (idx = 0; idx < context->name_count; idx++) {
  1520. struct audit_names *n = &context->names[idx];
  1521. if (!n->name)
  1522. continue;
  1523. if (n->ino == parent->i_ino &&
  1524. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1525. n->name_len = dirlen; /* update parent data in place */
  1526. found_parent = n->name;
  1527. goto add_names;
  1528. }
  1529. }
  1530. /* no matching parent, look for matching child */
  1531. for (idx = 0; idx < context->name_count; idx++) {
  1532. struct audit_names *n = &context->names[idx];
  1533. if (!n->name)
  1534. continue;
  1535. /* strcmp() is the more likely scenario */
  1536. if (!strcmp(dname, n->name) ||
  1537. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1538. if (inode)
  1539. audit_copy_inode(n, inode);
  1540. else
  1541. n->ino = (unsigned long)-1;
  1542. found_child = n->name;
  1543. goto add_names;
  1544. }
  1545. }
  1546. add_names:
  1547. if (!found_parent) {
  1548. if (audit_inc_name_count(context, parent))
  1549. return;
  1550. idx = context->name_count - 1;
  1551. context->names[idx].name = NULL;
  1552. audit_copy_inode(&context->names[idx], parent);
  1553. }
  1554. if (!found_child) {
  1555. if (audit_inc_name_count(context, inode))
  1556. return;
  1557. idx = context->name_count - 1;
  1558. /* Re-use the name belonging to the slot for a matching parent
  1559. * directory. All names for this context are relinquished in
  1560. * audit_free_names() */
  1561. if (found_parent) {
  1562. context->names[idx].name = found_parent;
  1563. context->names[idx].name_len = AUDIT_NAME_FULL;
  1564. /* don't call __putname() */
  1565. context->names[idx].name_put = 0;
  1566. } else {
  1567. context->names[idx].name = NULL;
  1568. }
  1569. if (inode)
  1570. audit_copy_inode(&context->names[idx], inode);
  1571. else
  1572. context->names[idx].ino = (unsigned long)-1;
  1573. }
  1574. }
  1575. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1576. /**
  1577. * auditsc_get_stamp - get local copies of audit_context values
  1578. * @ctx: audit_context for the task
  1579. * @t: timespec to store time recorded in the audit_context
  1580. * @serial: serial value that is recorded in the audit_context
  1581. *
  1582. * Also sets the context as auditable.
  1583. */
  1584. void auditsc_get_stamp(struct audit_context *ctx,
  1585. struct timespec *t, unsigned int *serial)
  1586. {
  1587. if (!ctx->serial)
  1588. ctx->serial = audit_serial();
  1589. t->tv_sec = ctx->ctime.tv_sec;
  1590. t->tv_nsec = ctx->ctime.tv_nsec;
  1591. *serial = ctx->serial;
  1592. ctx->auditable = 1;
  1593. }
  1594. /**
  1595. * audit_set_loginuid - set a task's audit_context loginuid
  1596. * @task: task whose audit context is being modified
  1597. * @loginuid: loginuid value
  1598. *
  1599. * Returns 0.
  1600. *
  1601. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1602. */
  1603. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1604. {
  1605. struct audit_context *context = task->audit_context;
  1606. if (context) {
  1607. /* Only log if audit is enabled */
  1608. if (context->in_syscall) {
  1609. struct audit_buffer *ab;
  1610. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1611. if (ab) {
  1612. audit_log_format(ab, "login pid=%d uid=%u "
  1613. "old auid=%u new auid=%u",
  1614. task->pid, task->uid,
  1615. context->loginuid, loginuid);
  1616. audit_log_end(ab);
  1617. }
  1618. }
  1619. context->loginuid = loginuid;
  1620. }
  1621. return 0;
  1622. }
  1623. /**
  1624. * audit_get_loginuid - get the loginuid for an audit_context
  1625. * @ctx: the audit_context
  1626. *
  1627. * Returns the context's loginuid or -1 if @ctx is NULL.
  1628. */
  1629. uid_t audit_get_loginuid(struct audit_context *ctx)
  1630. {
  1631. return ctx ? ctx->loginuid : -1;
  1632. }
  1633. EXPORT_SYMBOL(audit_get_loginuid);
  1634. /**
  1635. * __audit_mq_open - record audit data for a POSIX MQ open
  1636. * @oflag: open flag
  1637. * @mode: mode bits
  1638. * @u_attr: queue attributes
  1639. *
  1640. * Returns 0 for success or NULL context or < 0 on error.
  1641. */
  1642. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1643. {
  1644. struct audit_aux_data_mq_open *ax;
  1645. struct audit_context *context = current->audit_context;
  1646. if (!audit_enabled)
  1647. return 0;
  1648. if (likely(!context))
  1649. return 0;
  1650. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1651. if (!ax)
  1652. return -ENOMEM;
  1653. if (u_attr != NULL) {
  1654. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1655. kfree(ax);
  1656. return -EFAULT;
  1657. }
  1658. } else
  1659. memset(&ax->attr, 0, sizeof(ax->attr));
  1660. ax->oflag = oflag;
  1661. ax->mode = mode;
  1662. ax->d.type = AUDIT_MQ_OPEN;
  1663. ax->d.next = context->aux;
  1664. context->aux = (void *)ax;
  1665. return 0;
  1666. }
  1667. /**
  1668. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1669. * @mqdes: MQ descriptor
  1670. * @msg_len: Message length
  1671. * @msg_prio: Message priority
  1672. * @u_abs_timeout: Message timeout in absolute time
  1673. *
  1674. * Returns 0 for success or NULL context or < 0 on error.
  1675. */
  1676. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1677. const struct timespec __user *u_abs_timeout)
  1678. {
  1679. struct audit_aux_data_mq_sendrecv *ax;
  1680. struct audit_context *context = current->audit_context;
  1681. if (!audit_enabled)
  1682. return 0;
  1683. if (likely(!context))
  1684. return 0;
  1685. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1686. if (!ax)
  1687. return -ENOMEM;
  1688. if (u_abs_timeout != NULL) {
  1689. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1690. kfree(ax);
  1691. return -EFAULT;
  1692. }
  1693. } else
  1694. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1695. ax->mqdes = mqdes;
  1696. ax->msg_len = msg_len;
  1697. ax->msg_prio = msg_prio;
  1698. ax->d.type = AUDIT_MQ_SENDRECV;
  1699. ax->d.next = context->aux;
  1700. context->aux = (void *)ax;
  1701. return 0;
  1702. }
  1703. /**
  1704. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1705. * @mqdes: MQ descriptor
  1706. * @msg_len: Message length
  1707. * @u_msg_prio: Message priority
  1708. * @u_abs_timeout: Message timeout in absolute time
  1709. *
  1710. * Returns 0 for success or NULL context or < 0 on error.
  1711. */
  1712. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1713. unsigned int __user *u_msg_prio,
  1714. const struct timespec __user *u_abs_timeout)
  1715. {
  1716. struct audit_aux_data_mq_sendrecv *ax;
  1717. struct audit_context *context = current->audit_context;
  1718. if (!audit_enabled)
  1719. return 0;
  1720. if (likely(!context))
  1721. return 0;
  1722. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1723. if (!ax)
  1724. return -ENOMEM;
  1725. if (u_msg_prio != NULL) {
  1726. if (get_user(ax->msg_prio, u_msg_prio)) {
  1727. kfree(ax);
  1728. return -EFAULT;
  1729. }
  1730. } else
  1731. ax->msg_prio = 0;
  1732. if (u_abs_timeout != NULL) {
  1733. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1734. kfree(ax);
  1735. return -EFAULT;
  1736. }
  1737. } else
  1738. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1739. ax->mqdes = mqdes;
  1740. ax->msg_len = msg_len;
  1741. ax->d.type = AUDIT_MQ_SENDRECV;
  1742. ax->d.next = context->aux;
  1743. context->aux = (void *)ax;
  1744. return 0;
  1745. }
  1746. /**
  1747. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1748. * @mqdes: MQ descriptor
  1749. * @u_notification: Notification event
  1750. *
  1751. * Returns 0 for success or NULL context or < 0 on error.
  1752. */
  1753. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1754. {
  1755. struct audit_aux_data_mq_notify *ax;
  1756. struct audit_context *context = current->audit_context;
  1757. if (!audit_enabled)
  1758. return 0;
  1759. if (likely(!context))
  1760. return 0;
  1761. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1762. if (!ax)
  1763. return -ENOMEM;
  1764. if (u_notification != NULL) {
  1765. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1766. kfree(ax);
  1767. return -EFAULT;
  1768. }
  1769. } else
  1770. memset(&ax->notification, 0, sizeof(ax->notification));
  1771. ax->mqdes = mqdes;
  1772. ax->d.type = AUDIT_MQ_NOTIFY;
  1773. ax->d.next = context->aux;
  1774. context->aux = (void *)ax;
  1775. return 0;
  1776. }
  1777. /**
  1778. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1779. * @mqdes: MQ descriptor
  1780. * @mqstat: MQ flags
  1781. *
  1782. * Returns 0 for success or NULL context or < 0 on error.
  1783. */
  1784. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1785. {
  1786. struct audit_aux_data_mq_getsetattr *ax;
  1787. struct audit_context *context = current->audit_context;
  1788. if (!audit_enabled)
  1789. return 0;
  1790. if (likely(!context))
  1791. return 0;
  1792. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1793. if (!ax)
  1794. return -ENOMEM;
  1795. ax->mqdes = mqdes;
  1796. ax->mqstat = *mqstat;
  1797. ax->d.type = AUDIT_MQ_GETSETATTR;
  1798. ax->d.next = context->aux;
  1799. context->aux = (void *)ax;
  1800. return 0;
  1801. }
  1802. /**
  1803. * audit_ipc_obj - record audit data for ipc object
  1804. * @ipcp: ipc permissions
  1805. *
  1806. * Returns 0 for success or NULL context or < 0 on error.
  1807. */
  1808. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1809. {
  1810. struct audit_aux_data_ipcctl *ax;
  1811. struct audit_context *context = current->audit_context;
  1812. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1813. if (!ax)
  1814. return -ENOMEM;
  1815. ax->uid = ipcp->uid;
  1816. ax->gid = ipcp->gid;
  1817. ax->mode = ipcp->mode;
  1818. selinux_get_ipc_sid(ipcp, &ax->osid);
  1819. ax->d.type = AUDIT_IPC;
  1820. ax->d.next = context->aux;
  1821. context->aux = (void *)ax;
  1822. return 0;
  1823. }
  1824. /**
  1825. * audit_ipc_set_perm - record audit data for new ipc permissions
  1826. * @qbytes: msgq bytes
  1827. * @uid: msgq user id
  1828. * @gid: msgq group id
  1829. * @mode: msgq mode (permissions)
  1830. *
  1831. * Returns 0 for success or NULL context or < 0 on error.
  1832. */
  1833. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1834. {
  1835. struct audit_aux_data_ipcctl *ax;
  1836. struct audit_context *context = current->audit_context;
  1837. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1838. if (!ax)
  1839. return -ENOMEM;
  1840. ax->qbytes = qbytes;
  1841. ax->uid = uid;
  1842. ax->gid = gid;
  1843. ax->mode = mode;
  1844. ax->d.type = AUDIT_IPC_SET_PERM;
  1845. ax->d.next = context->aux;
  1846. context->aux = (void *)ax;
  1847. return 0;
  1848. }
  1849. int audit_argv_kb = 32;
  1850. int audit_bprm(struct linux_binprm *bprm)
  1851. {
  1852. struct audit_aux_data_execve *ax;
  1853. struct audit_context *context = current->audit_context;
  1854. if (likely(!audit_enabled || !context || context->dummy))
  1855. return 0;
  1856. /*
  1857. * Even though the stack code doesn't limit the arg+env size any more,
  1858. * the audit code requires that _all_ arguments be logged in a single
  1859. * netlink skb. Hence cap it :-(
  1860. */
  1861. if (bprm->argv_len > (audit_argv_kb << 10))
  1862. return -E2BIG;
  1863. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1864. if (!ax)
  1865. return -ENOMEM;
  1866. ax->argc = bprm->argc;
  1867. ax->envc = bprm->envc;
  1868. ax->mm = bprm->mm;
  1869. ax->d.type = AUDIT_EXECVE;
  1870. ax->d.next = context->aux;
  1871. context->aux = (void *)ax;
  1872. return 0;
  1873. }
  1874. /**
  1875. * audit_socketcall - record audit data for sys_socketcall
  1876. * @nargs: number of args
  1877. * @args: args array
  1878. *
  1879. * Returns 0 for success or NULL context or < 0 on error.
  1880. */
  1881. int audit_socketcall(int nargs, unsigned long *args)
  1882. {
  1883. struct audit_aux_data_socketcall *ax;
  1884. struct audit_context *context = current->audit_context;
  1885. if (likely(!context || context->dummy))
  1886. return 0;
  1887. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1888. if (!ax)
  1889. return -ENOMEM;
  1890. ax->nargs = nargs;
  1891. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1892. ax->d.type = AUDIT_SOCKETCALL;
  1893. ax->d.next = context->aux;
  1894. context->aux = (void *)ax;
  1895. return 0;
  1896. }
  1897. /**
  1898. * __audit_fd_pair - record audit data for pipe and socketpair
  1899. * @fd1: the first file descriptor
  1900. * @fd2: the second file descriptor
  1901. *
  1902. * Returns 0 for success or NULL context or < 0 on error.
  1903. */
  1904. int __audit_fd_pair(int fd1, int fd2)
  1905. {
  1906. struct audit_context *context = current->audit_context;
  1907. struct audit_aux_data_fd_pair *ax;
  1908. if (likely(!context)) {
  1909. return 0;
  1910. }
  1911. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1912. if (!ax) {
  1913. return -ENOMEM;
  1914. }
  1915. ax->fd[0] = fd1;
  1916. ax->fd[1] = fd2;
  1917. ax->d.type = AUDIT_FD_PAIR;
  1918. ax->d.next = context->aux;
  1919. context->aux = (void *)ax;
  1920. return 0;
  1921. }
  1922. /**
  1923. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1924. * @len: data length in user space
  1925. * @a: data address in kernel space
  1926. *
  1927. * Returns 0 for success or NULL context or < 0 on error.
  1928. */
  1929. int audit_sockaddr(int len, void *a)
  1930. {
  1931. struct audit_aux_data_sockaddr *ax;
  1932. struct audit_context *context = current->audit_context;
  1933. if (likely(!context || context->dummy))
  1934. return 0;
  1935. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1936. if (!ax)
  1937. return -ENOMEM;
  1938. ax->len = len;
  1939. memcpy(ax->a, a, len);
  1940. ax->d.type = AUDIT_SOCKADDR;
  1941. ax->d.next = context->aux;
  1942. context->aux = (void *)ax;
  1943. return 0;
  1944. }
  1945. void __audit_ptrace(struct task_struct *t)
  1946. {
  1947. struct audit_context *context = current->audit_context;
  1948. context->target_pid = t->pid;
  1949. selinux_get_task_sid(t, &context->target_sid);
  1950. }
  1951. /**
  1952. * audit_signal_info - record signal info for shutting down audit subsystem
  1953. * @sig: signal value
  1954. * @t: task being signaled
  1955. *
  1956. * If the audit subsystem is being terminated, record the task (pid)
  1957. * and uid that is doing that.
  1958. */
  1959. int __audit_signal_info(int sig, struct task_struct *t)
  1960. {
  1961. struct audit_aux_data_pids *axp;
  1962. struct task_struct *tsk = current;
  1963. struct audit_context *ctx = tsk->audit_context;
  1964. extern pid_t audit_sig_pid;
  1965. extern uid_t audit_sig_uid;
  1966. extern u32 audit_sig_sid;
  1967. if (audit_pid && t->tgid == audit_pid) {
  1968. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1969. audit_sig_pid = tsk->pid;
  1970. if (ctx)
  1971. audit_sig_uid = ctx->loginuid;
  1972. else
  1973. audit_sig_uid = tsk->uid;
  1974. selinux_get_task_sid(tsk, &audit_sig_sid);
  1975. }
  1976. if (!audit_signals || audit_dummy_context())
  1977. return 0;
  1978. }
  1979. /* optimize the common case by putting first signal recipient directly
  1980. * in audit_context */
  1981. if (!ctx->target_pid) {
  1982. ctx->target_pid = t->tgid;
  1983. selinux_get_task_sid(t, &ctx->target_sid);
  1984. return 0;
  1985. }
  1986. axp = (void *)ctx->aux_pids;
  1987. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  1988. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  1989. if (!axp)
  1990. return -ENOMEM;
  1991. axp->d.type = AUDIT_OBJ_PID;
  1992. axp->d.next = ctx->aux_pids;
  1993. ctx->aux_pids = (void *)axp;
  1994. }
  1995. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  1996. axp->target_pid[axp->pid_count] = t->tgid;
  1997. selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
  1998. axp->pid_count++;
  1999. return 0;
  2000. }
  2001. /**
  2002. * audit_core_dumps - record information about processes that end abnormally
  2003. * @signr: signal value
  2004. *
  2005. * If a process ends with a core dump, something fishy is going on and we
  2006. * should record the event for investigation.
  2007. */
  2008. void audit_core_dumps(long signr)
  2009. {
  2010. struct audit_buffer *ab;
  2011. u32 sid;
  2012. if (!audit_enabled)
  2013. return;
  2014. if (signr == SIGQUIT) /* don't care for those */
  2015. return;
  2016. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2017. audit_log_format(ab, "auid=%u uid=%u gid=%u",
  2018. audit_get_loginuid(current->audit_context),
  2019. current->uid, current->gid);
  2020. selinux_get_task_sid(current, &sid);
  2021. if (sid) {
  2022. char *ctx = NULL;
  2023. u32 len;
  2024. if (selinux_sid_to_string(sid, &ctx, &len))
  2025. audit_log_format(ab, " ssid=%u", sid);
  2026. else
  2027. audit_log_format(ab, " subj=%s", ctx);
  2028. kfree(ctx);
  2029. }
  2030. audit_log_format(ab, " pid=%d comm=", current->pid);
  2031. audit_log_untrustedstring(ab, current->comm);
  2032. audit_log_format(ab, " sig=%ld", signr);
  2033. audit_log_end(ab);
  2034. }