auditsc.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  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 <linux/atomic.h>
  47. #include <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/mm.h>
  50. #include <linux/export.h>
  51. #include <linux/slab.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/binfmts.h>
  65. #include <linux/highmem.h>
  66. #include <linux/syscalls.h>
  67. #include <linux/capability.h>
  68. #include <linux/fs_struct.h>
  69. #include "audit.h"
  70. /* flags stating the success for a syscall */
  71. #define AUDITSC_INVALID 0
  72. #define AUDITSC_SUCCESS 1
  73. #define AUDITSC_FAILURE 2
  74. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  75. * for saving names from getname(). If we get more names we will allocate
  76. * a name dynamically and also add those to the list anchored by names_list. */
  77. #define AUDIT_NAMES 5
  78. /* Indicates that audit should log the full pathname. */
  79. #define AUDIT_NAME_FULL -1
  80. /* no execve audit message should be longer than this (userspace limits) */
  81. #define MAX_EXECVE_AUDIT_LEN 7500
  82. /* number of audit rules */
  83. int audit_n_rules;
  84. /* determines whether we collect data for signals sent */
  85. int audit_signals;
  86. struct audit_cap_data {
  87. kernel_cap_t permitted;
  88. kernel_cap_t inheritable;
  89. union {
  90. unsigned int fE; /* effective bit of a file capability */
  91. kernel_cap_t effective; /* effective set of a process */
  92. };
  93. };
  94. /* When fs/namei.c:getname() is called, we store the pointer in name and
  95. * we don't let putname() free it (instead we free all of the saved
  96. * pointers at syscall exit time).
  97. *
  98. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  99. struct audit_names {
  100. struct list_head list; /* audit_context->names_list */
  101. const char *name;
  102. unsigned long ino;
  103. dev_t dev;
  104. umode_t mode;
  105. uid_t uid;
  106. gid_t gid;
  107. dev_t rdev;
  108. u32 osid;
  109. struct audit_cap_data fcap;
  110. unsigned int fcap_ver;
  111. int name_len; /* number of name's characters to log */
  112. bool name_put; /* call __putname() for this name */
  113. /*
  114. * This was an allocated audit_names and not from the array of
  115. * names allocated in the task audit context. Thus this name
  116. * should be freed on syscall exit
  117. */
  118. bool should_free;
  119. };
  120. struct audit_aux_data {
  121. struct audit_aux_data *next;
  122. int type;
  123. };
  124. #define AUDIT_AUX_IPCPERM 0
  125. /* Number of target pids per aux struct. */
  126. #define AUDIT_AUX_PIDS 16
  127. struct audit_aux_data_execve {
  128. struct audit_aux_data d;
  129. int argc;
  130. int envc;
  131. struct mm_struct *mm;
  132. };
  133. struct audit_aux_data_pids {
  134. struct audit_aux_data d;
  135. pid_t target_pid[AUDIT_AUX_PIDS];
  136. uid_t target_auid[AUDIT_AUX_PIDS];
  137. uid_t target_uid[AUDIT_AUX_PIDS];
  138. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  139. u32 target_sid[AUDIT_AUX_PIDS];
  140. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  141. int pid_count;
  142. };
  143. struct audit_aux_data_bprm_fcaps {
  144. struct audit_aux_data d;
  145. struct audit_cap_data fcap;
  146. unsigned int fcap_ver;
  147. struct audit_cap_data old_pcap;
  148. struct audit_cap_data new_pcap;
  149. };
  150. struct audit_aux_data_capset {
  151. struct audit_aux_data d;
  152. pid_t pid;
  153. struct audit_cap_data cap;
  154. };
  155. struct audit_tree_refs {
  156. struct audit_tree_refs *next;
  157. struct audit_chunk *c[31];
  158. };
  159. /* The per-task audit context. */
  160. struct audit_context {
  161. int dummy; /* must be the first element */
  162. int in_syscall; /* 1 if task is in a syscall */
  163. enum audit_state state, current_state;
  164. unsigned int serial; /* serial number for record */
  165. int major; /* syscall number */
  166. struct timespec ctime; /* time of syscall entry */
  167. unsigned long argv[4]; /* syscall arguments */
  168. long return_code;/* syscall return code */
  169. u64 prio;
  170. int return_valid; /* return code is valid */
  171. /*
  172. * The names_list is the list of all audit_names collected during this
  173. * syscall. The first AUDIT_NAMES entries in the names_list will
  174. * actually be from the preallocated_names array for performance
  175. * reasons. Except during allocation they should never be referenced
  176. * through the preallocated_names array and should only be found/used
  177. * by running the names_list.
  178. */
  179. struct audit_names preallocated_names[AUDIT_NAMES];
  180. int name_count; /* total records in names_list */
  181. struct list_head names_list; /* anchor for struct audit_names->list */
  182. char * filterkey; /* key for rule that triggered record */
  183. struct path pwd;
  184. struct audit_context *previous; /* For nested syscalls */
  185. struct audit_aux_data *aux;
  186. struct audit_aux_data *aux_pids;
  187. struct sockaddr_storage *sockaddr;
  188. size_t sockaddr_len;
  189. /* Save things to print about task_struct */
  190. pid_t pid, ppid;
  191. uid_t uid, euid, suid, fsuid;
  192. gid_t gid, egid, sgid, fsgid;
  193. unsigned long personality;
  194. int arch;
  195. pid_t target_pid;
  196. uid_t target_auid;
  197. uid_t target_uid;
  198. unsigned int target_sessionid;
  199. u32 target_sid;
  200. char target_comm[TASK_COMM_LEN];
  201. struct audit_tree_refs *trees, *first_trees;
  202. struct list_head killed_trees;
  203. int tree_count;
  204. int type;
  205. union {
  206. struct {
  207. int nargs;
  208. long args[6];
  209. } socketcall;
  210. struct {
  211. uid_t uid;
  212. gid_t gid;
  213. umode_t mode;
  214. u32 osid;
  215. int has_perm;
  216. uid_t perm_uid;
  217. gid_t perm_gid;
  218. umode_t perm_mode;
  219. unsigned long qbytes;
  220. } ipc;
  221. struct {
  222. mqd_t mqdes;
  223. struct mq_attr mqstat;
  224. } mq_getsetattr;
  225. struct {
  226. mqd_t mqdes;
  227. int sigev_signo;
  228. } mq_notify;
  229. struct {
  230. mqd_t mqdes;
  231. size_t msg_len;
  232. unsigned int msg_prio;
  233. struct timespec abs_timeout;
  234. } mq_sendrecv;
  235. struct {
  236. int oflag;
  237. umode_t mode;
  238. struct mq_attr attr;
  239. } mq_open;
  240. struct {
  241. pid_t pid;
  242. struct audit_cap_data cap;
  243. } capset;
  244. struct {
  245. int fd;
  246. int flags;
  247. } mmap;
  248. };
  249. int fds[2];
  250. #if AUDIT_DEBUG
  251. int put_count;
  252. int ino_count;
  253. #endif
  254. };
  255. static inline int open_arg(int flags, int mask)
  256. {
  257. int n = ACC_MODE(flags);
  258. if (flags & (O_TRUNC | O_CREAT))
  259. n |= AUDIT_PERM_WRITE;
  260. return n & mask;
  261. }
  262. static int audit_match_perm(struct audit_context *ctx, int mask)
  263. {
  264. unsigned n;
  265. if (unlikely(!ctx))
  266. return 0;
  267. n = ctx->major;
  268. switch (audit_classify_syscall(ctx->arch, n)) {
  269. case 0: /* native */
  270. if ((mask & AUDIT_PERM_WRITE) &&
  271. audit_match_class(AUDIT_CLASS_WRITE, n))
  272. return 1;
  273. if ((mask & AUDIT_PERM_READ) &&
  274. audit_match_class(AUDIT_CLASS_READ, n))
  275. return 1;
  276. if ((mask & AUDIT_PERM_ATTR) &&
  277. audit_match_class(AUDIT_CLASS_CHATTR, n))
  278. return 1;
  279. return 0;
  280. case 1: /* 32bit on biarch */
  281. if ((mask & AUDIT_PERM_WRITE) &&
  282. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  283. return 1;
  284. if ((mask & AUDIT_PERM_READ) &&
  285. audit_match_class(AUDIT_CLASS_READ_32, n))
  286. return 1;
  287. if ((mask & AUDIT_PERM_ATTR) &&
  288. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  289. return 1;
  290. return 0;
  291. case 2: /* open */
  292. return mask & ACC_MODE(ctx->argv[1]);
  293. case 3: /* openat */
  294. return mask & ACC_MODE(ctx->argv[2]);
  295. case 4: /* socketcall */
  296. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  297. case 5: /* execve */
  298. return mask & AUDIT_PERM_EXEC;
  299. default:
  300. return 0;
  301. }
  302. }
  303. static int audit_match_filetype(struct audit_context *ctx, int val)
  304. {
  305. struct audit_names *n;
  306. umode_t mode = (umode_t)val;
  307. if (unlikely(!ctx))
  308. return 0;
  309. list_for_each_entry(n, &ctx->names_list, list) {
  310. if ((n->ino != -1) &&
  311. ((n->mode & S_IFMT) == mode))
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. /*
  317. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  318. * ->first_trees points to its beginning, ->trees - to the current end of data.
  319. * ->tree_count is the number of free entries in array pointed to by ->trees.
  320. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  321. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  322. * it's going to remain 1-element for almost any setup) until we free context itself.
  323. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  324. */
  325. #ifdef CONFIG_AUDIT_TREE
  326. static void audit_set_auditable(struct audit_context *ctx)
  327. {
  328. if (!ctx->prio) {
  329. ctx->prio = 1;
  330. ctx->current_state = AUDIT_RECORD_CONTEXT;
  331. }
  332. }
  333. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  334. {
  335. struct audit_tree_refs *p = ctx->trees;
  336. int left = ctx->tree_count;
  337. if (likely(left)) {
  338. p->c[--left] = chunk;
  339. ctx->tree_count = left;
  340. return 1;
  341. }
  342. if (!p)
  343. return 0;
  344. p = p->next;
  345. if (p) {
  346. p->c[30] = chunk;
  347. ctx->trees = p;
  348. ctx->tree_count = 30;
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. static int grow_tree_refs(struct audit_context *ctx)
  354. {
  355. struct audit_tree_refs *p = ctx->trees;
  356. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  357. if (!ctx->trees) {
  358. ctx->trees = p;
  359. return 0;
  360. }
  361. if (p)
  362. p->next = ctx->trees;
  363. else
  364. ctx->first_trees = ctx->trees;
  365. ctx->tree_count = 31;
  366. return 1;
  367. }
  368. #endif
  369. static void unroll_tree_refs(struct audit_context *ctx,
  370. struct audit_tree_refs *p, int count)
  371. {
  372. #ifdef CONFIG_AUDIT_TREE
  373. struct audit_tree_refs *q;
  374. int n;
  375. if (!p) {
  376. /* we started with empty chain */
  377. p = ctx->first_trees;
  378. count = 31;
  379. /* if the very first allocation has failed, nothing to do */
  380. if (!p)
  381. return;
  382. }
  383. n = count;
  384. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  385. while (n--) {
  386. audit_put_chunk(q->c[n]);
  387. q->c[n] = NULL;
  388. }
  389. }
  390. while (n-- > ctx->tree_count) {
  391. audit_put_chunk(q->c[n]);
  392. q->c[n] = NULL;
  393. }
  394. ctx->trees = p;
  395. ctx->tree_count = count;
  396. #endif
  397. }
  398. static void free_tree_refs(struct audit_context *ctx)
  399. {
  400. struct audit_tree_refs *p, *q;
  401. for (p = ctx->first_trees; p; p = q) {
  402. q = p->next;
  403. kfree(p);
  404. }
  405. }
  406. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  407. {
  408. #ifdef CONFIG_AUDIT_TREE
  409. struct audit_tree_refs *p;
  410. int n;
  411. if (!tree)
  412. return 0;
  413. /* full ones */
  414. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  415. for (n = 0; n < 31; n++)
  416. if (audit_tree_match(p->c[n], tree))
  417. return 1;
  418. }
  419. /* partial */
  420. if (p) {
  421. for (n = ctx->tree_count; n < 31; n++)
  422. if (audit_tree_match(p->c[n], tree))
  423. return 1;
  424. }
  425. #endif
  426. return 0;
  427. }
  428. static int audit_field_compare(struct task_struct *tsk,
  429. const struct cred *cred,
  430. struct audit_field *f,
  431. struct audit_context *ctx,
  432. struct audit_names *name)
  433. {
  434. struct audit_names *n;
  435. switch (f->val) {
  436. case AUDIT_COMPARE_UID_TO_OBJ_UID:
  437. if (name) {
  438. return audit_comparator(cred->uid, f->op, name->uid);
  439. } else if (ctx) {
  440. list_for_each_entry(n, &ctx->names_list, list) {
  441. if (audit_comparator(cred->uid, f->op, n->uid))
  442. return 1;
  443. }
  444. }
  445. break;
  446. default:
  447. WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
  448. return 0;
  449. }
  450. return 0;
  451. }
  452. /* Determine if any context name data matches a rule's watch data */
  453. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  454. * otherwise.
  455. *
  456. * If task_creation is true, this is an explicit indication that we are
  457. * filtering a task rule at task creation time. This and tsk == current are
  458. * the only situations where tsk->cred may be accessed without an rcu read lock.
  459. */
  460. static int audit_filter_rules(struct task_struct *tsk,
  461. struct audit_krule *rule,
  462. struct audit_context *ctx,
  463. struct audit_names *name,
  464. enum audit_state *state,
  465. bool task_creation)
  466. {
  467. const struct cred *cred;
  468. int i, need_sid = 1;
  469. u32 sid;
  470. cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
  471. for (i = 0; i < rule->field_count; i++) {
  472. struct audit_field *f = &rule->fields[i];
  473. struct audit_names *n;
  474. int result = 0;
  475. switch (f->type) {
  476. case AUDIT_PID:
  477. result = audit_comparator(tsk->pid, f->op, f->val);
  478. break;
  479. case AUDIT_PPID:
  480. if (ctx) {
  481. if (!ctx->ppid)
  482. ctx->ppid = sys_getppid();
  483. result = audit_comparator(ctx->ppid, f->op, f->val);
  484. }
  485. break;
  486. case AUDIT_UID:
  487. result = audit_comparator(cred->uid, f->op, f->val);
  488. break;
  489. case AUDIT_EUID:
  490. result = audit_comparator(cred->euid, f->op, f->val);
  491. break;
  492. case AUDIT_SUID:
  493. result = audit_comparator(cred->suid, f->op, f->val);
  494. break;
  495. case AUDIT_FSUID:
  496. result = audit_comparator(cred->fsuid, f->op, f->val);
  497. break;
  498. case AUDIT_GID:
  499. result = audit_comparator(cred->gid, f->op, f->val);
  500. break;
  501. case AUDIT_EGID:
  502. result = audit_comparator(cred->egid, f->op, f->val);
  503. break;
  504. case AUDIT_SGID:
  505. result = audit_comparator(cred->sgid, f->op, f->val);
  506. break;
  507. case AUDIT_FSGID:
  508. result = audit_comparator(cred->fsgid, f->op, f->val);
  509. break;
  510. case AUDIT_PERS:
  511. result = audit_comparator(tsk->personality, f->op, f->val);
  512. break;
  513. case AUDIT_ARCH:
  514. if (ctx)
  515. result = audit_comparator(ctx->arch, f->op, f->val);
  516. break;
  517. case AUDIT_EXIT:
  518. if (ctx && ctx->return_valid)
  519. result = audit_comparator(ctx->return_code, f->op, f->val);
  520. break;
  521. case AUDIT_SUCCESS:
  522. if (ctx && ctx->return_valid) {
  523. if (f->val)
  524. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  525. else
  526. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  527. }
  528. break;
  529. case AUDIT_DEVMAJOR:
  530. if (name) {
  531. if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
  532. audit_comparator(MAJOR(name->rdev), f->op, f->val))
  533. ++result;
  534. } else if (ctx) {
  535. list_for_each_entry(n, &ctx->names_list, list) {
  536. if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
  537. audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
  538. ++result;
  539. break;
  540. }
  541. }
  542. }
  543. break;
  544. case AUDIT_DEVMINOR:
  545. if (name) {
  546. if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
  547. audit_comparator(MINOR(name->rdev), f->op, f->val))
  548. ++result;
  549. } else if (ctx) {
  550. list_for_each_entry(n, &ctx->names_list, list) {
  551. if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
  552. audit_comparator(MINOR(n->rdev), f->op, f->val)) {
  553. ++result;
  554. break;
  555. }
  556. }
  557. }
  558. break;
  559. case AUDIT_INODE:
  560. if (name)
  561. result = (name->ino == f->val);
  562. else if (ctx) {
  563. list_for_each_entry(n, &ctx->names_list, list) {
  564. if (audit_comparator(n->ino, f->op, f->val)) {
  565. ++result;
  566. break;
  567. }
  568. }
  569. }
  570. break;
  571. case AUDIT_OBJ_UID:
  572. if (name) {
  573. result = audit_comparator(name->uid, f->op, f->val);
  574. } else if (ctx) {
  575. list_for_each_entry(n, &ctx->names_list, list) {
  576. if (audit_comparator(n->uid, f->op, f->val)) {
  577. ++result;
  578. break;
  579. }
  580. }
  581. }
  582. break;
  583. case AUDIT_OBJ_GID:
  584. if (name) {
  585. result = audit_comparator(name->gid, f->op, f->val);
  586. } else if (ctx) {
  587. list_for_each_entry(n, &ctx->names_list, list) {
  588. if (audit_comparator(n->gid, f->op, f->val)) {
  589. ++result;
  590. break;
  591. }
  592. }
  593. }
  594. break;
  595. case AUDIT_WATCH:
  596. if (name)
  597. result = audit_watch_compare(rule->watch, name->ino, name->dev);
  598. break;
  599. case AUDIT_DIR:
  600. if (ctx)
  601. result = match_tree_refs(ctx, rule->tree);
  602. break;
  603. case AUDIT_LOGINUID:
  604. result = 0;
  605. if (ctx)
  606. result = audit_comparator(tsk->loginuid, f->op, f->val);
  607. break;
  608. case AUDIT_SUBJ_USER:
  609. case AUDIT_SUBJ_ROLE:
  610. case AUDIT_SUBJ_TYPE:
  611. case AUDIT_SUBJ_SEN:
  612. case AUDIT_SUBJ_CLR:
  613. /* NOTE: this may return negative values indicating
  614. a temporary error. We simply treat this as a
  615. match for now to avoid losing information that
  616. may be wanted. An error message will also be
  617. logged upon error */
  618. if (f->lsm_rule) {
  619. if (need_sid) {
  620. security_task_getsecid(tsk, &sid);
  621. need_sid = 0;
  622. }
  623. result = security_audit_rule_match(sid, f->type,
  624. f->op,
  625. f->lsm_rule,
  626. ctx);
  627. }
  628. break;
  629. case AUDIT_OBJ_USER:
  630. case AUDIT_OBJ_ROLE:
  631. case AUDIT_OBJ_TYPE:
  632. case AUDIT_OBJ_LEV_LOW:
  633. case AUDIT_OBJ_LEV_HIGH:
  634. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  635. also applies here */
  636. if (f->lsm_rule) {
  637. /* Find files that match */
  638. if (name) {
  639. result = security_audit_rule_match(
  640. name->osid, f->type, f->op,
  641. f->lsm_rule, ctx);
  642. } else if (ctx) {
  643. list_for_each_entry(n, &ctx->names_list, list) {
  644. if (security_audit_rule_match(n->osid, f->type,
  645. f->op, f->lsm_rule,
  646. ctx)) {
  647. ++result;
  648. break;
  649. }
  650. }
  651. }
  652. /* Find ipc objects that match */
  653. if (!ctx || ctx->type != AUDIT_IPC)
  654. break;
  655. if (security_audit_rule_match(ctx->ipc.osid,
  656. f->type, f->op,
  657. f->lsm_rule, ctx))
  658. ++result;
  659. }
  660. break;
  661. case AUDIT_ARG0:
  662. case AUDIT_ARG1:
  663. case AUDIT_ARG2:
  664. case AUDIT_ARG3:
  665. if (ctx)
  666. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  667. break;
  668. case AUDIT_FILTERKEY:
  669. /* ignore this field for filtering */
  670. result = 1;
  671. break;
  672. case AUDIT_PERM:
  673. result = audit_match_perm(ctx, f->val);
  674. break;
  675. case AUDIT_FILETYPE:
  676. result = audit_match_filetype(ctx, f->val);
  677. break;
  678. case AUDIT_FIELD_COMPARE:
  679. result = audit_field_compare(tsk, cred, f, ctx, name);
  680. break;
  681. }
  682. if (!result)
  683. return 0;
  684. }
  685. if (ctx) {
  686. if (rule->prio <= ctx->prio)
  687. return 0;
  688. if (rule->filterkey) {
  689. kfree(ctx->filterkey);
  690. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  691. }
  692. ctx->prio = rule->prio;
  693. }
  694. switch (rule->action) {
  695. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  696. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  697. }
  698. return 1;
  699. }
  700. /* At process creation time, we can determine if system-call auditing is
  701. * completely disabled for this task. Since we only have the task
  702. * structure at this point, we can only check uid and gid.
  703. */
  704. static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
  705. {
  706. struct audit_entry *e;
  707. enum audit_state state;
  708. rcu_read_lock();
  709. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  710. if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
  711. &state, true)) {
  712. if (state == AUDIT_RECORD_CONTEXT)
  713. *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
  714. rcu_read_unlock();
  715. return state;
  716. }
  717. }
  718. rcu_read_unlock();
  719. return AUDIT_BUILD_CONTEXT;
  720. }
  721. /* At syscall entry and exit time, this filter is called if the
  722. * audit_state is not low enough that auditing cannot take place, but is
  723. * also not high enough that we already know we have to write an audit
  724. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  725. */
  726. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  727. struct audit_context *ctx,
  728. struct list_head *list)
  729. {
  730. struct audit_entry *e;
  731. enum audit_state state;
  732. if (audit_pid && tsk->tgid == audit_pid)
  733. return AUDIT_DISABLED;
  734. rcu_read_lock();
  735. if (!list_empty(list)) {
  736. int word = AUDIT_WORD(ctx->major);
  737. int bit = AUDIT_BIT(ctx->major);
  738. list_for_each_entry_rcu(e, list, list) {
  739. if ((e->rule.mask[word] & bit) == bit &&
  740. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  741. &state, false)) {
  742. rcu_read_unlock();
  743. ctx->current_state = state;
  744. return state;
  745. }
  746. }
  747. }
  748. rcu_read_unlock();
  749. return AUDIT_BUILD_CONTEXT;
  750. }
  751. /*
  752. * Given an audit_name check the inode hash table to see if they match.
  753. * Called holding the rcu read lock to protect the use of audit_inode_hash
  754. */
  755. static int audit_filter_inode_name(struct task_struct *tsk,
  756. struct audit_names *n,
  757. struct audit_context *ctx) {
  758. int word, bit;
  759. int h = audit_hash_ino((u32)n->ino);
  760. struct list_head *list = &audit_inode_hash[h];
  761. struct audit_entry *e;
  762. enum audit_state state;
  763. word = AUDIT_WORD(ctx->major);
  764. bit = AUDIT_BIT(ctx->major);
  765. if (list_empty(list))
  766. return 0;
  767. list_for_each_entry_rcu(e, list, list) {
  768. if ((e->rule.mask[word] & bit) == bit &&
  769. audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
  770. ctx->current_state = state;
  771. return 1;
  772. }
  773. }
  774. return 0;
  775. }
  776. /* At syscall exit time, this filter is called if any audit_names have been
  777. * collected during syscall processing. We only check rules in sublists at hash
  778. * buckets applicable to the inode numbers in audit_names.
  779. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  780. */
  781. void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
  782. {
  783. struct audit_names *n;
  784. if (audit_pid && tsk->tgid == audit_pid)
  785. return;
  786. rcu_read_lock();
  787. list_for_each_entry(n, &ctx->names_list, list) {
  788. if (audit_filter_inode_name(tsk, n, ctx))
  789. break;
  790. }
  791. rcu_read_unlock();
  792. }
  793. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  794. int return_valid,
  795. long return_code)
  796. {
  797. struct audit_context *context = tsk->audit_context;
  798. if (!context)
  799. return NULL;
  800. context->return_valid = return_valid;
  801. /*
  802. * we need to fix up the return code in the audit logs if the actual
  803. * return codes are later going to be fixed up by the arch specific
  804. * signal handlers
  805. *
  806. * This is actually a test for:
  807. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  808. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  809. *
  810. * but is faster than a bunch of ||
  811. */
  812. if (unlikely(return_code <= -ERESTARTSYS) &&
  813. (return_code >= -ERESTART_RESTARTBLOCK) &&
  814. (return_code != -ENOIOCTLCMD))
  815. context->return_code = -EINTR;
  816. else
  817. context->return_code = return_code;
  818. if (context->in_syscall && !context->dummy) {
  819. audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  820. audit_filter_inodes(tsk, context);
  821. }
  822. tsk->audit_context = NULL;
  823. return context;
  824. }
  825. static inline void audit_free_names(struct audit_context *context)
  826. {
  827. struct audit_names *n, *next;
  828. #if AUDIT_DEBUG == 2
  829. if (context->put_count + context->ino_count != context->name_count) {
  830. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  831. " name_count=%d put_count=%d"
  832. " ino_count=%d [NOT freeing]\n",
  833. __FILE__, __LINE__,
  834. context->serial, context->major, context->in_syscall,
  835. context->name_count, context->put_count,
  836. context->ino_count);
  837. list_for_each_entry(n, &context->names_list, list) {
  838. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  839. n->name, n->name ?: "(null)");
  840. }
  841. dump_stack();
  842. return;
  843. }
  844. #endif
  845. #if AUDIT_DEBUG
  846. context->put_count = 0;
  847. context->ino_count = 0;
  848. #endif
  849. list_for_each_entry_safe(n, next, &context->names_list, list) {
  850. list_del(&n->list);
  851. if (n->name && n->name_put)
  852. __putname(n->name);
  853. if (n->should_free)
  854. kfree(n);
  855. }
  856. context->name_count = 0;
  857. path_put(&context->pwd);
  858. context->pwd.dentry = NULL;
  859. context->pwd.mnt = NULL;
  860. }
  861. static inline void audit_free_aux(struct audit_context *context)
  862. {
  863. struct audit_aux_data *aux;
  864. while ((aux = context->aux)) {
  865. context->aux = aux->next;
  866. kfree(aux);
  867. }
  868. while ((aux = context->aux_pids)) {
  869. context->aux_pids = aux->next;
  870. kfree(aux);
  871. }
  872. }
  873. static inline void audit_zero_context(struct audit_context *context,
  874. enum audit_state state)
  875. {
  876. memset(context, 0, sizeof(*context));
  877. context->state = state;
  878. context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  879. }
  880. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  881. {
  882. struct audit_context *context;
  883. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  884. return NULL;
  885. audit_zero_context(context, state);
  886. INIT_LIST_HEAD(&context->killed_trees);
  887. INIT_LIST_HEAD(&context->names_list);
  888. return context;
  889. }
  890. /**
  891. * audit_alloc - allocate an audit context block for a task
  892. * @tsk: task
  893. *
  894. * Filter on the task information and allocate a per-task audit context
  895. * if necessary. Doing so turns on system call auditing for the
  896. * specified task. This is called from copy_process, so no lock is
  897. * needed.
  898. */
  899. int audit_alloc(struct task_struct *tsk)
  900. {
  901. struct audit_context *context;
  902. enum audit_state state;
  903. char *key = NULL;
  904. if (likely(!audit_ever_enabled))
  905. return 0; /* Return if not auditing. */
  906. state = audit_filter_task(tsk, &key);
  907. if (state == AUDIT_DISABLED)
  908. return 0;
  909. if (!(context = audit_alloc_context(state))) {
  910. kfree(key);
  911. audit_log_lost("out of memory in audit_alloc");
  912. return -ENOMEM;
  913. }
  914. context->filterkey = key;
  915. tsk->audit_context = context;
  916. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  917. return 0;
  918. }
  919. static inline void audit_free_context(struct audit_context *context)
  920. {
  921. struct audit_context *previous;
  922. int count = 0;
  923. do {
  924. previous = context->previous;
  925. if (previous || (count && count < 10)) {
  926. ++count;
  927. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  928. " freeing multiple contexts (%d)\n",
  929. context->serial, context->major,
  930. context->name_count, count);
  931. }
  932. audit_free_names(context);
  933. unroll_tree_refs(context, NULL, 0);
  934. free_tree_refs(context);
  935. audit_free_aux(context);
  936. kfree(context->filterkey);
  937. kfree(context->sockaddr);
  938. kfree(context);
  939. context = previous;
  940. } while (context);
  941. if (count >= 10)
  942. printk(KERN_ERR "audit: freed %d contexts\n", count);
  943. }
  944. void audit_log_task_context(struct audit_buffer *ab)
  945. {
  946. char *ctx = NULL;
  947. unsigned len;
  948. int error;
  949. u32 sid;
  950. security_task_getsecid(current, &sid);
  951. if (!sid)
  952. return;
  953. error = security_secid_to_secctx(sid, &ctx, &len);
  954. if (error) {
  955. if (error != -EINVAL)
  956. goto error_path;
  957. return;
  958. }
  959. audit_log_format(ab, " subj=%s", ctx);
  960. security_release_secctx(ctx, len);
  961. return;
  962. error_path:
  963. audit_panic("error in audit_log_task_context");
  964. return;
  965. }
  966. EXPORT_SYMBOL(audit_log_task_context);
  967. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  968. {
  969. char name[sizeof(tsk->comm)];
  970. struct mm_struct *mm = tsk->mm;
  971. struct vm_area_struct *vma;
  972. /* tsk == current */
  973. get_task_comm(name, tsk);
  974. audit_log_format(ab, " comm=");
  975. audit_log_untrustedstring(ab, name);
  976. if (mm) {
  977. down_read(&mm->mmap_sem);
  978. vma = mm->mmap;
  979. while (vma) {
  980. if ((vma->vm_flags & VM_EXECUTABLE) &&
  981. vma->vm_file) {
  982. audit_log_d_path(ab, "exe=",
  983. &vma->vm_file->f_path);
  984. break;
  985. }
  986. vma = vma->vm_next;
  987. }
  988. up_read(&mm->mmap_sem);
  989. }
  990. audit_log_task_context(ab);
  991. }
  992. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  993. uid_t auid, uid_t uid, unsigned int sessionid,
  994. u32 sid, char *comm)
  995. {
  996. struct audit_buffer *ab;
  997. char *ctx = NULL;
  998. u32 len;
  999. int rc = 0;
  1000. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  1001. if (!ab)
  1002. return rc;
  1003. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
  1004. uid, sessionid);
  1005. if (security_secid_to_secctx(sid, &ctx, &len)) {
  1006. audit_log_format(ab, " obj=(none)");
  1007. rc = 1;
  1008. } else {
  1009. audit_log_format(ab, " obj=%s", ctx);
  1010. security_release_secctx(ctx, len);
  1011. }
  1012. audit_log_format(ab, " ocomm=");
  1013. audit_log_untrustedstring(ab, comm);
  1014. audit_log_end(ab);
  1015. return rc;
  1016. }
  1017. /*
  1018. * to_send and len_sent accounting are very loose estimates. We aren't
  1019. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  1020. * within about 500 bytes (next page boundary)
  1021. *
  1022. * why snprintf? an int is up to 12 digits long. if we just assumed when
  1023. * logging that a[%d]= was going to be 16 characters long we would be wasting
  1024. * space in every audit message. In one 7500 byte message we can log up to
  1025. * about 1000 min size arguments. That comes down to about 50% waste of space
  1026. * if we didn't do the snprintf to find out how long arg_num_len was.
  1027. */
  1028. static int audit_log_single_execve_arg(struct audit_context *context,
  1029. struct audit_buffer **ab,
  1030. int arg_num,
  1031. size_t *len_sent,
  1032. const char __user *p,
  1033. char *buf)
  1034. {
  1035. char arg_num_len_buf[12];
  1036. const char __user *tmp_p = p;
  1037. /* how many digits are in arg_num? 5 is the length of ' a=""' */
  1038. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
  1039. size_t len, len_left, to_send;
  1040. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  1041. unsigned int i, has_cntl = 0, too_long = 0;
  1042. int ret;
  1043. /* strnlen_user includes the null we don't want to send */
  1044. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  1045. /*
  1046. * We just created this mm, if we can't find the strings
  1047. * we just copied into it something is _very_ wrong. Similar
  1048. * for strings that are too long, we should not have created
  1049. * any.
  1050. */
  1051. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  1052. WARN_ON(1);
  1053. send_sig(SIGKILL, current, 0);
  1054. return -1;
  1055. }
  1056. /* walk the whole argument looking for non-ascii chars */
  1057. do {
  1058. if (len_left > MAX_EXECVE_AUDIT_LEN)
  1059. to_send = MAX_EXECVE_AUDIT_LEN;
  1060. else
  1061. to_send = len_left;
  1062. ret = copy_from_user(buf, tmp_p, to_send);
  1063. /*
  1064. * There is no reason for this copy to be short. We just
  1065. * copied them here, and the mm hasn't been exposed to user-
  1066. * space yet.
  1067. */
  1068. if (ret) {
  1069. WARN_ON(1);
  1070. send_sig(SIGKILL, current, 0);
  1071. return -1;
  1072. }
  1073. buf[to_send] = '\0';
  1074. has_cntl = audit_string_contains_control(buf, to_send);
  1075. if (has_cntl) {
  1076. /*
  1077. * hex messages get logged as 2 bytes, so we can only
  1078. * send half as much in each message
  1079. */
  1080. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  1081. break;
  1082. }
  1083. len_left -= to_send;
  1084. tmp_p += to_send;
  1085. } while (len_left > 0);
  1086. len_left = len;
  1087. if (len > max_execve_audit_len)
  1088. too_long = 1;
  1089. /* rewalk the argument actually logging the message */
  1090. for (i = 0; len_left > 0; i++) {
  1091. int room_left;
  1092. if (len_left > max_execve_audit_len)
  1093. to_send = max_execve_audit_len;
  1094. else
  1095. to_send = len_left;
  1096. /* do we have space left to send this argument in this ab? */
  1097. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  1098. if (has_cntl)
  1099. room_left -= (to_send * 2);
  1100. else
  1101. room_left -= to_send;
  1102. if (room_left < 0) {
  1103. *len_sent = 0;
  1104. audit_log_end(*ab);
  1105. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  1106. if (!*ab)
  1107. return 0;
  1108. }
  1109. /*
  1110. * first record needs to say how long the original string was
  1111. * so we can be sure nothing was lost.
  1112. */
  1113. if ((i == 0) && (too_long))
  1114. audit_log_format(*ab, " a%d_len=%zu", arg_num,
  1115. has_cntl ? 2*len : len);
  1116. /*
  1117. * normally arguments are small enough to fit and we already
  1118. * filled buf above when we checked for control characters
  1119. * so don't bother with another copy_from_user
  1120. */
  1121. if (len >= max_execve_audit_len)
  1122. ret = copy_from_user(buf, p, to_send);
  1123. else
  1124. ret = 0;
  1125. if (ret) {
  1126. WARN_ON(1);
  1127. send_sig(SIGKILL, current, 0);
  1128. return -1;
  1129. }
  1130. buf[to_send] = '\0';
  1131. /* actually log it */
  1132. audit_log_format(*ab, " a%d", arg_num);
  1133. if (too_long)
  1134. audit_log_format(*ab, "[%d]", i);
  1135. audit_log_format(*ab, "=");
  1136. if (has_cntl)
  1137. audit_log_n_hex(*ab, buf, to_send);
  1138. else
  1139. audit_log_string(*ab, buf);
  1140. p += to_send;
  1141. len_left -= to_send;
  1142. *len_sent += arg_num_len;
  1143. if (has_cntl)
  1144. *len_sent += to_send * 2;
  1145. else
  1146. *len_sent += to_send;
  1147. }
  1148. /* include the null we didn't log */
  1149. return len + 1;
  1150. }
  1151. static void audit_log_execve_info(struct audit_context *context,
  1152. struct audit_buffer **ab,
  1153. struct audit_aux_data_execve *axi)
  1154. {
  1155. int i;
  1156. size_t len, len_sent = 0;
  1157. const char __user *p;
  1158. char *buf;
  1159. if (axi->mm != current->mm)
  1160. return; /* execve failed, no additional info */
  1161. p = (const char __user *)axi->mm->arg_start;
  1162. audit_log_format(*ab, "argc=%d", axi->argc);
  1163. /*
  1164. * we need some kernel buffer to hold the userspace args. Just
  1165. * allocate one big one rather than allocating one of the right size
  1166. * for every single argument inside audit_log_single_execve_arg()
  1167. * should be <8k allocation so should be pretty safe.
  1168. */
  1169. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1170. if (!buf) {
  1171. audit_panic("out of memory for argv string\n");
  1172. return;
  1173. }
  1174. for (i = 0; i < axi->argc; i++) {
  1175. len = audit_log_single_execve_arg(context, ab, i,
  1176. &len_sent, p, buf);
  1177. if (len <= 0)
  1178. break;
  1179. p += len;
  1180. }
  1181. kfree(buf);
  1182. }
  1183. static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
  1184. {
  1185. int i;
  1186. audit_log_format(ab, " %s=", prefix);
  1187. CAP_FOR_EACH_U32(i) {
  1188. audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
  1189. }
  1190. }
  1191. static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
  1192. {
  1193. kernel_cap_t *perm = &name->fcap.permitted;
  1194. kernel_cap_t *inh = &name->fcap.inheritable;
  1195. int log = 0;
  1196. if (!cap_isclear(*perm)) {
  1197. audit_log_cap(ab, "cap_fp", perm);
  1198. log = 1;
  1199. }
  1200. if (!cap_isclear(*inh)) {
  1201. audit_log_cap(ab, "cap_fi", inh);
  1202. log = 1;
  1203. }
  1204. if (log)
  1205. audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
  1206. }
  1207. static void show_special(struct audit_context *context, int *call_panic)
  1208. {
  1209. struct audit_buffer *ab;
  1210. int i;
  1211. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1212. if (!ab)
  1213. return;
  1214. switch (context->type) {
  1215. case AUDIT_SOCKETCALL: {
  1216. int nargs = context->socketcall.nargs;
  1217. audit_log_format(ab, "nargs=%d", nargs);
  1218. for (i = 0; i < nargs; i++)
  1219. audit_log_format(ab, " a%d=%lx", i,
  1220. context->socketcall.args[i]);
  1221. break; }
  1222. case AUDIT_IPC: {
  1223. u32 osid = context->ipc.osid;
  1224. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1225. context->ipc.uid, context->ipc.gid, context->ipc.mode);
  1226. if (osid) {
  1227. char *ctx = NULL;
  1228. u32 len;
  1229. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1230. audit_log_format(ab, " osid=%u", osid);
  1231. *call_panic = 1;
  1232. } else {
  1233. audit_log_format(ab, " obj=%s", ctx);
  1234. security_release_secctx(ctx, len);
  1235. }
  1236. }
  1237. if (context->ipc.has_perm) {
  1238. audit_log_end(ab);
  1239. ab = audit_log_start(context, GFP_KERNEL,
  1240. AUDIT_IPC_SET_PERM);
  1241. audit_log_format(ab,
  1242. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1243. context->ipc.qbytes,
  1244. context->ipc.perm_uid,
  1245. context->ipc.perm_gid,
  1246. context->ipc.perm_mode);
  1247. if (!ab)
  1248. return;
  1249. }
  1250. break; }
  1251. case AUDIT_MQ_OPEN: {
  1252. audit_log_format(ab,
  1253. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1254. "mq_msgsize=%ld mq_curmsgs=%ld",
  1255. context->mq_open.oflag, context->mq_open.mode,
  1256. context->mq_open.attr.mq_flags,
  1257. context->mq_open.attr.mq_maxmsg,
  1258. context->mq_open.attr.mq_msgsize,
  1259. context->mq_open.attr.mq_curmsgs);
  1260. break; }
  1261. case AUDIT_MQ_SENDRECV: {
  1262. audit_log_format(ab,
  1263. "mqdes=%d msg_len=%zd msg_prio=%u "
  1264. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1265. context->mq_sendrecv.mqdes,
  1266. context->mq_sendrecv.msg_len,
  1267. context->mq_sendrecv.msg_prio,
  1268. context->mq_sendrecv.abs_timeout.tv_sec,
  1269. context->mq_sendrecv.abs_timeout.tv_nsec);
  1270. break; }
  1271. case AUDIT_MQ_NOTIFY: {
  1272. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1273. context->mq_notify.mqdes,
  1274. context->mq_notify.sigev_signo);
  1275. break; }
  1276. case AUDIT_MQ_GETSETATTR: {
  1277. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1278. audit_log_format(ab,
  1279. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1280. "mq_curmsgs=%ld ",
  1281. context->mq_getsetattr.mqdes,
  1282. attr->mq_flags, attr->mq_maxmsg,
  1283. attr->mq_msgsize, attr->mq_curmsgs);
  1284. break; }
  1285. case AUDIT_CAPSET: {
  1286. audit_log_format(ab, "pid=%d", context->capset.pid);
  1287. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1288. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1289. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1290. break; }
  1291. case AUDIT_MMAP: {
  1292. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1293. context->mmap.flags);
  1294. break; }
  1295. }
  1296. audit_log_end(ab);
  1297. }
  1298. static void audit_log_name(struct audit_context *context, struct audit_names *n,
  1299. int record_num, int *call_panic)
  1300. {
  1301. struct audit_buffer *ab;
  1302. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1303. if (!ab)
  1304. return; /* audit_panic has been called */
  1305. audit_log_format(ab, "item=%d", record_num);
  1306. if (n->name) {
  1307. switch (n->name_len) {
  1308. case AUDIT_NAME_FULL:
  1309. /* log the full path */
  1310. audit_log_format(ab, " name=");
  1311. audit_log_untrustedstring(ab, n->name);
  1312. break;
  1313. case 0:
  1314. /* name was specified as a relative path and the
  1315. * directory component is the cwd */
  1316. audit_log_d_path(ab, "name=", &context->pwd);
  1317. break;
  1318. default:
  1319. /* log the name's directory component */
  1320. audit_log_format(ab, " name=");
  1321. audit_log_n_untrustedstring(ab, n->name,
  1322. n->name_len);
  1323. }
  1324. } else
  1325. audit_log_format(ab, " name=(null)");
  1326. if (n->ino != (unsigned long)-1) {
  1327. audit_log_format(ab, " inode=%lu"
  1328. " dev=%02x:%02x mode=%#ho"
  1329. " ouid=%u ogid=%u rdev=%02x:%02x",
  1330. n->ino,
  1331. MAJOR(n->dev),
  1332. MINOR(n->dev),
  1333. n->mode,
  1334. n->uid,
  1335. n->gid,
  1336. MAJOR(n->rdev),
  1337. MINOR(n->rdev));
  1338. }
  1339. if (n->osid != 0) {
  1340. char *ctx = NULL;
  1341. u32 len;
  1342. if (security_secid_to_secctx(
  1343. n->osid, &ctx, &len)) {
  1344. audit_log_format(ab, " osid=%u", n->osid);
  1345. *call_panic = 2;
  1346. } else {
  1347. audit_log_format(ab, " obj=%s", ctx);
  1348. security_release_secctx(ctx, len);
  1349. }
  1350. }
  1351. audit_log_fcaps(ab, n);
  1352. audit_log_end(ab);
  1353. }
  1354. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1355. {
  1356. const struct cred *cred;
  1357. int i, call_panic = 0;
  1358. struct audit_buffer *ab;
  1359. struct audit_aux_data *aux;
  1360. const char *tty;
  1361. struct audit_names *n;
  1362. /* tsk == current */
  1363. context->pid = tsk->pid;
  1364. if (!context->ppid)
  1365. context->ppid = sys_getppid();
  1366. cred = current_cred();
  1367. context->uid = cred->uid;
  1368. context->gid = cred->gid;
  1369. context->euid = cred->euid;
  1370. context->suid = cred->suid;
  1371. context->fsuid = cred->fsuid;
  1372. context->egid = cred->egid;
  1373. context->sgid = cred->sgid;
  1374. context->fsgid = cred->fsgid;
  1375. context->personality = tsk->personality;
  1376. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1377. if (!ab)
  1378. return; /* audit_panic has been called */
  1379. audit_log_format(ab, "arch=%x syscall=%d",
  1380. context->arch, context->major);
  1381. if (context->personality != PER_LINUX)
  1382. audit_log_format(ab, " per=%lx", context->personality);
  1383. if (context->return_valid)
  1384. audit_log_format(ab, " success=%s exit=%ld",
  1385. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1386. context->return_code);
  1387. spin_lock_irq(&tsk->sighand->siglock);
  1388. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  1389. tty = tsk->signal->tty->name;
  1390. else
  1391. tty = "(none)";
  1392. spin_unlock_irq(&tsk->sighand->siglock);
  1393. audit_log_format(ab,
  1394. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  1395. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  1396. " euid=%u suid=%u fsuid=%u"
  1397. " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
  1398. context->argv[0],
  1399. context->argv[1],
  1400. context->argv[2],
  1401. context->argv[3],
  1402. context->name_count,
  1403. context->ppid,
  1404. context->pid,
  1405. tsk->loginuid,
  1406. context->uid,
  1407. context->gid,
  1408. context->euid, context->suid, context->fsuid,
  1409. context->egid, context->sgid, context->fsgid, tty,
  1410. tsk->sessionid);
  1411. audit_log_task_info(ab, tsk);
  1412. audit_log_key(ab, context->filterkey);
  1413. audit_log_end(ab);
  1414. for (aux = context->aux; aux; aux = aux->next) {
  1415. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1416. if (!ab)
  1417. continue; /* audit_panic has been called */
  1418. switch (aux->type) {
  1419. case AUDIT_EXECVE: {
  1420. struct audit_aux_data_execve *axi = (void *)aux;
  1421. audit_log_execve_info(context, &ab, axi);
  1422. break; }
  1423. case AUDIT_BPRM_FCAPS: {
  1424. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1425. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1426. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1427. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1428. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1429. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1430. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1431. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1432. audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
  1433. audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
  1434. audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
  1435. break; }
  1436. }
  1437. audit_log_end(ab);
  1438. }
  1439. if (context->type)
  1440. show_special(context, &call_panic);
  1441. if (context->fds[0] >= 0) {
  1442. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1443. if (ab) {
  1444. audit_log_format(ab, "fd0=%d fd1=%d",
  1445. context->fds[0], context->fds[1]);
  1446. audit_log_end(ab);
  1447. }
  1448. }
  1449. if (context->sockaddr_len) {
  1450. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1451. if (ab) {
  1452. audit_log_format(ab, "saddr=");
  1453. audit_log_n_hex(ab, (void *)context->sockaddr,
  1454. context->sockaddr_len);
  1455. audit_log_end(ab);
  1456. }
  1457. }
  1458. for (aux = context->aux_pids; aux; aux = aux->next) {
  1459. struct audit_aux_data_pids *axs = (void *)aux;
  1460. for (i = 0; i < axs->pid_count; i++)
  1461. if (audit_log_pid_context(context, axs->target_pid[i],
  1462. axs->target_auid[i],
  1463. axs->target_uid[i],
  1464. axs->target_sessionid[i],
  1465. axs->target_sid[i],
  1466. axs->target_comm[i]))
  1467. call_panic = 1;
  1468. }
  1469. if (context->target_pid &&
  1470. audit_log_pid_context(context, context->target_pid,
  1471. context->target_auid, context->target_uid,
  1472. context->target_sessionid,
  1473. context->target_sid, context->target_comm))
  1474. call_panic = 1;
  1475. if (context->pwd.dentry && context->pwd.mnt) {
  1476. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1477. if (ab) {
  1478. audit_log_d_path(ab, "cwd=", &context->pwd);
  1479. audit_log_end(ab);
  1480. }
  1481. }
  1482. i = 0;
  1483. list_for_each_entry(n, &context->names_list, list)
  1484. audit_log_name(context, n, i++, &call_panic);
  1485. /* Send end of event record to help user space know we are finished */
  1486. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1487. if (ab)
  1488. audit_log_end(ab);
  1489. if (call_panic)
  1490. audit_panic("error converting sid to string");
  1491. }
  1492. /**
  1493. * audit_free - free a per-task audit context
  1494. * @tsk: task whose audit context block to free
  1495. *
  1496. * Called from copy_process and do_exit
  1497. */
  1498. void __audit_free(struct task_struct *tsk)
  1499. {
  1500. struct audit_context *context;
  1501. context = audit_get_context(tsk, 0, 0);
  1502. if (!context)
  1503. return;
  1504. /* Check for system calls that do not go through the exit
  1505. * function (e.g., exit_group), then free context block.
  1506. * We use GFP_ATOMIC here because we might be doing this
  1507. * in the context of the idle thread */
  1508. /* that can happen only if we are called from do_exit() */
  1509. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1510. audit_log_exit(context, tsk);
  1511. if (!list_empty(&context->killed_trees))
  1512. audit_kill_trees(&context->killed_trees);
  1513. audit_free_context(context);
  1514. }
  1515. /**
  1516. * audit_syscall_entry - fill in an audit record at syscall entry
  1517. * @arch: architecture type
  1518. * @major: major syscall type (function)
  1519. * @a1: additional syscall register 1
  1520. * @a2: additional syscall register 2
  1521. * @a3: additional syscall register 3
  1522. * @a4: additional syscall register 4
  1523. *
  1524. * Fill in audit context at syscall entry. This only happens if the
  1525. * audit context was created when the task was created and the state or
  1526. * filters demand the audit context be built. If the state from the
  1527. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1528. * then the record will be written at syscall exit time (otherwise, it
  1529. * will only be written if another part of the kernel requests that it
  1530. * be written).
  1531. */
  1532. void __audit_syscall_entry(int arch, int major,
  1533. unsigned long a1, unsigned long a2,
  1534. unsigned long a3, unsigned long a4)
  1535. {
  1536. struct task_struct *tsk = current;
  1537. struct audit_context *context = tsk->audit_context;
  1538. enum audit_state state;
  1539. if (!context)
  1540. return;
  1541. /*
  1542. * This happens only on certain architectures that make system
  1543. * calls in kernel_thread via the entry.S interface, instead of
  1544. * with direct calls. (If you are porting to a new
  1545. * architecture, hitting this condition can indicate that you
  1546. * got the _exit/_leave calls backward in entry.S.)
  1547. *
  1548. * i386 no
  1549. * x86_64 no
  1550. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  1551. *
  1552. * This also happens with vm86 emulation in a non-nested manner
  1553. * (entries without exits), so this case must be caught.
  1554. */
  1555. if (context->in_syscall) {
  1556. struct audit_context *newctx;
  1557. #if AUDIT_DEBUG
  1558. printk(KERN_ERR
  1559. "audit(:%d) pid=%d in syscall=%d;"
  1560. " entering syscall=%d\n",
  1561. context->serial, tsk->pid, context->major, major);
  1562. #endif
  1563. newctx = audit_alloc_context(context->state);
  1564. if (newctx) {
  1565. newctx->previous = context;
  1566. context = newctx;
  1567. tsk->audit_context = newctx;
  1568. } else {
  1569. /* If we can't alloc a new context, the best we
  1570. * can do is to leak memory (any pending putname
  1571. * will be lost). The only other alternative is
  1572. * to abandon auditing. */
  1573. audit_zero_context(context, context->state);
  1574. }
  1575. }
  1576. BUG_ON(context->in_syscall || context->name_count);
  1577. if (!audit_enabled)
  1578. return;
  1579. context->arch = arch;
  1580. context->major = major;
  1581. context->argv[0] = a1;
  1582. context->argv[1] = a2;
  1583. context->argv[2] = a3;
  1584. context->argv[3] = a4;
  1585. state = context->state;
  1586. context->dummy = !audit_n_rules;
  1587. if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
  1588. context->prio = 0;
  1589. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1590. }
  1591. if (state == AUDIT_DISABLED)
  1592. return;
  1593. context->serial = 0;
  1594. context->ctime = CURRENT_TIME;
  1595. context->in_syscall = 1;
  1596. context->current_state = state;
  1597. context->ppid = 0;
  1598. }
  1599. /**
  1600. * audit_syscall_exit - deallocate audit context after a system call
  1601. * @pt_regs: syscall registers
  1602. *
  1603. * Tear down after system call. If the audit context has been marked as
  1604. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1605. * filtering, or because some other part of the kernel write an audit
  1606. * message), then write out the syscall information. In call cases,
  1607. * free the names stored from getname().
  1608. */
  1609. void __audit_syscall_exit(int success, long return_code)
  1610. {
  1611. struct task_struct *tsk = current;
  1612. struct audit_context *context;
  1613. if (success)
  1614. success = AUDITSC_SUCCESS;
  1615. else
  1616. success = AUDITSC_FAILURE;
  1617. context = audit_get_context(tsk, success, return_code);
  1618. if (!context)
  1619. return;
  1620. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1621. audit_log_exit(context, tsk);
  1622. context->in_syscall = 0;
  1623. context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  1624. if (!list_empty(&context->killed_trees))
  1625. audit_kill_trees(&context->killed_trees);
  1626. if (context->previous) {
  1627. struct audit_context *new_context = context->previous;
  1628. context->previous = NULL;
  1629. audit_free_context(context);
  1630. tsk->audit_context = new_context;
  1631. } else {
  1632. audit_free_names(context);
  1633. unroll_tree_refs(context, NULL, 0);
  1634. audit_free_aux(context);
  1635. context->aux = NULL;
  1636. context->aux_pids = NULL;
  1637. context->target_pid = 0;
  1638. context->target_sid = 0;
  1639. context->sockaddr_len = 0;
  1640. context->type = 0;
  1641. context->fds[0] = -1;
  1642. if (context->state != AUDIT_RECORD_CONTEXT) {
  1643. kfree(context->filterkey);
  1644. context->filterkey = NULL;
  1645. }
  1646. tsk->audit_context = context;
  1647. }
  1648. }
  1649. static inline void handle_one(const struct inode *inode)
  1650. {
  1651. #ifdef CONFIG_AUDIT_TREE
  1652. struct audit_context *context;
  1653. struct audit_tree_refs *p;
  1654. struct audit_chunk *chunk;
  1655. int count;
  1656. if (likely(hlist_empty(&inode->i_fsnotify_marks)))
  1657. return;
  1658. context = current->audit_context;
  1659. p = context->trees;
  1660. count = context->tree_count;
  1661. rcu_read_lock();
  1662. chunk = audit_tree_lookup(inode);
  1663. rcu_read_unlock();
  1664. if (!chunk)
  1665. return;
  1666. if (likely(put_tree_ref(context, chunk)))
  1667. return;
  1668. if (unlikely(!grow_tree_refs(context))) {
  1669. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1670. audit_set_auditable(context);
  1671. audit_put_chunk(chunk);
  1672. unroll_tree_refs(context, p, count);
  1673. return;
  1674. }
  1675. put_tree_ref(context, chunk);
  1676. #endif
  1677. }
  1678. static void handle_path(const struct dentry *dentry)
  1679. {
  1680. #ifdef CONFIG_AUDIT_TREE
  1681. struct audit_context *context;
  1682. struct audit_tree_refs *p;
  1683. const struct dentry *d, *parent;
  1684. struct audit_chunk *drop;
  1685. unsigned long seq;
  1686. int count;
  1687. context = current->audit_context;
  1688. p = context->trees;
  1689. count = context->tree_count;
  1690. retry:
  1691. drop = NULL;
  1692. d = dentry;
  1693. rcu_read_lock();
  1694. seq = read_seqbegin(&rename_lock);
  1695. for(;;) {
  1696. struct inode *inode = d->d_inode;
  1697. if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
  1698. struct audit_chunk *chunk;
  1699. chunk = audit_tree_lookup(inode);
  1700. if (chunk) {
  1701. if (unlikely(!put_tree_ref(context, chunk))) {
  1702. drop = chunk;
  1703. break;
  1704. }
  1705. }
  1706. }
  1707. parent = d->d_parent;
  1708. if (parent == d)
  1709. break;
  1710. d = parent;
  1711. }
  1712. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1713. rcu_read_unlock();
  1714. if (!drop) {
  1715. /* just a race with rename */
  1716. unroll_tree_refs(context, p, count);
  1717. goto retry;
  1718. }
  1719. audit_put_chunk(drop);
  1720. if (grow_tree_refs(context)) {
  1721. /* OK, got more space */
  1722. unroll_tree_refs(context, p, count);
  1723. goto retry;
  1724. }
  1725. /* too bad */
  1726. printk(KERN_WARNING
  1727. "out of memory, audit has lost a tree reference\n");
  1728. unroll_tree_refs(context, p, count);
  1729. audit_set_auditable(context);
  1730. return;
  1731. }
  1732. rcu_read_unlock();
  1733. #endif
  1734. }
  1735. static struct audit_names *audit_alloc_name(struct audit_context *context)
  1736. {
  1737. struct audit_names *aname;
  1738. if (context->name_count < AUDIT_NAMES) {
  1739. aname = &context->preallocated_names[context->name_count];
  1740. memset(aname, 0, sizeof(*aname));
  1741. } else {
  1742. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1743. if (!aname)
  1744. return NULL;
  1745. aname->should_free = true;
  1746. }
  1747. aname->ino = (unsigned long)-1;
  1748. list_add_tail(&aname->list, &context->names_list);
  1749. context->name_count++;
  1750. #if AUDIT_DEBUG
  1751. context->ino_count++;
  1752. #endif
  1753. return aname;
  1754. }
  1755. /**
  1756. * audit_getname - add a name to the list
  1757. * @name: name to add
  1758. *
  1759. * Add a name to the list of audit names for this context.
  1760. * Called from fs/namei.c:getname().
  1761. */
  1762. void __audit_getname(const char *name)
  1763. {
  1764. struct audit_context *context = current->audit_context;
  1765. struct audit_names *n;
  1766. if (!context->in_syscall) {
  1767. #if AUDIT_DEBUG == 2
  1768. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1769. __FILE__, __LINE__, context->serial, name);
  1770. dump_stack();
  1771. #endif
  1772. return;
  1773. }
  1774. n = audit_alloc_name(context);
  1775. if (!n)
  1776. return;
  1777. n->name = name;
  1778. n->name_len = AUDIT_NAME_FULL;
  1779. n->name_put = true;
  1780. if (!context->pwd.dentry)
  1781. get_fs_pwd(current->fs, &context->pwd);
  1782. }
  1783. /* audit_putname - intercept a putname request
  1784. * @name: name to intercept and delay for putname
  1785. *
  1786. * If we have stored the name from getname in the audit context,
  1787. * then we delay the putname until syscall exit.
  1788. * Called from include/linux/fs.h:putname().
  1789. */
  1790. void audit_putname(const char *name)
  1791. {
  1792. struct audit_context *context = current->audit_context;
  1793. BUG_ON(!context);
  1794. if (!context->in_syscall) {
  1795. #if AUDIT_DEBUG == 2
  1796. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1797. __FILE__, __LINE__, context->serial, name);
  1798. if (context->name_count) {
  1799. struct audit_names *n;
  1800. int i;
  1801. list_for_each_entry(n, &context->names_list, list)
  1802. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1803. n->name, n->name ?: "(null)");
  1804. }
  1805. #endif
  1806. __putname(name);
  1807. }
  1808. #if AUDIT_DEBUG
  1809. else {
  1810. ++context->put_count;
  1811. if (context->put_count > context->name_count) {
  1812. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1813. " in_syscall=%d putname(%p) name_count=%d"
  1814. " put_count=%d\n",
  1815. __FILE__, __LINE__,
  1816. context->serial, context->major,
  1817. context->in_syscall, name, context->name_count,
  1818. context->put_count);
  1819. dump_stack();
  1820. }
  1821. }
  1822. #endif
  1823. }
  1824. static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
  1825. {
  1826. struct cpu_vfs_cap_data caps;
  1827. int rc;
  1828. if (!dentry)
  1829. return 0;
  1830. rc = get_vfs_caps_from_disk(dentry, &caps);
  1831. if (rc)
  1832. return rc;
  1833. name->fcap.permitted = caps.permitted;
  1834. name->fcap.inheritable = caps.inheritable;
  1835. name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  1836. name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  1837. return 0;
  1838. }
  1839. /* Copy inode data into an audit_names. */
  1840. static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
  1841. const struct inode *inode)
  1842. {
  1843. name->ino = inode->i_ino;
  1844. name->dev = inode->i_sb->s_dev;
  1845. name->mode = inode->i_mode;
  1846. name->uid = inode->i_uid;
  1847. name->gid = inode->i_gid;
  1848. name->rdev = inode->i_rdev;
  1849. security_inode_getsecid(inode, &name->osid);
  1850. audit_copy_fcaps(name, dentry);
  1851. }
  1852. /**
  1853. * audit_inode - store the inode and device from a lookup
  1854. * @name: name being audited
  1855. * @dentry: dentry being audited
  1856. *
  1857. * Called from fs/namei.c:path_lookup().
  1858. */
  1859. void __audit_inode(const char *name, const struct dentry *dentry)
  1860. {
  1861. struct audit_context *context = current->audit_context;
  1862. const struct inode *inode = dentry->d_inode;
  1863. struct audit_names *n;
  1864. if (!context->in_syscall)
  1865. return;
  1866. list_for_each_entry_reverse(n, &context->names_list, list) {
  1867. if (n->name && (n->name == name))
  1868. goto out;
  1869. }
  1870. /* unable to find the name from a previous getname() */
  1871. n = audit_alloc_name(context);
  1872. if (!n)
  1873. return;
  1874. out:
  1875. handle_path(dentry);
  1876. audit_copy_inode(n, dentry, inode);
  1877. }
  1878. /**
  1879. * audit_inode_child - collect inode info for created/removed objects
  1880. * @dentry: dentry being audited
  1881. * @parent: inode of dentry parent
  1882. *
  1883. * For syscalls that create or remove filesystem objects, audit_inode
  1884. * can only collect information for the filesystem object's parent.
  1885. * This call updates the audit context with the child's information.
  1886. * Syscalls that create a new filesystem object must be hooked after
  1887. * the object is created. Syscalls that remove a filesystem object
  1888. * must be hooked prior, in order to capture the target inode during
  1889. * unsuccessful attempts.
  1890. */
  1891. void __audit_inode_child(const struct dentry *dentry,
  1892. const struct inode *parent)
  1893. {
  1894. struct audit_context *context = current->audit_context;
  1895. const char *found_parent = NULL, *found_child = NULL;
  1896. const struct inode *inode = dentry->d_inode;
  1897. const char *dname = dentry->d_name.name;
  1898. struct audit_names *n;
  1899. int dirlen = 0;
  1900. if (!context->in_syscall)
  1901. return;
  1902. if (inode)
  1903. handle_one(inode);
  1904. /* parent is more likely, look for it first */
  1905. list_for_each_entry(n, &context->names_list, list) {
  1906. if (!n->name)
  1907. continue;
  1908. if (n->ino == parent->i_ino &&
  1909. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1910. n->name_len = dirlen; /* update parent data in place */
  1911. found_parent = n->name;
  1912. goto add_names;
  1913. }
  1914. }
  1915. /* no matching parent, look for matching child */
  1916. list_for_each_entry(n, &context->names_list, list) {
  1917. if (!n->name)
  1918. continue;
  1919. /* strcmp() is the more likely scenario */
  1920. if (!strcmp(dname, n->name) ||
  1921. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1922. if (inode)
  1923. audit_copy_inode(n, NULL, inode);
  1924. else
  1925. n->ino = (unsigned long)-1;
  1926. found_child = n->name;
  1927. goto add_names;
  1928. }
  1929. }
  1930. add_names:
  1931. if (!found_parent) {
  1932. n = audit_alloc_name(context);
  1933. if (!n)
  1934. return;
  1935. audit_copy_inode(n, NULL, parent);
  1936. }
  1937. if (!found_child) {
  1938. n = audit_alloc_name(context);
  1939. if (!n)
  1940. return;
  1941. /* Re-use the name belonging to the slot for a matching parent
  1942. * directory. All names for this context are relinquished in
  1943. * audit_free_names() */
  1944. if (found_parent) {
  1945. n->name = found_parent;
  1946. n->name_len = AUDIT_NAME_FULL;
  1947. /* don't call __putname() */
  1948. n->name_put = false;
  1949. }
  1950. if (inode)
  1951. audit_copy_inode(n, NULL, inode);
  1952. }
  1953. }
  1954. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1955. /**
  1956. * auditsc_get_stamp - get local copies of audit_context values
  1957. * @ctx: audit_context for the task
  1958. * @t: timespec to store time recorded in the audit_context
  1959. * @serial: serial value that is recorded in the audit_context
  1960. *
  1961. * Also sets the context as auditable.
  1962. */
  1963. int auditsc_get_stamp(struct audit_context *ctx,
  1964. struct timespec *t, unsigned int *serial)
  1965. {
  1966. if (!ctx->in_syscall)
  1967. return 0;
  1968. if (!ctx->serial)
  1969. ctx->serial = audit_serial();
  1970. t->tv_sec = ctx->ctime.tv_sec;
  1971. t->tv_nsec = ctx->ctime.tv_nsec;
  1972. *serial = ctx->serial;
  1973. if (!ctx->prio) {
  1974. ctx->prio = 1;
  1975. ctx->current_state = AUDIT_RECORD_CONTEXT;
  1976. }
  1977. return 1;
  1978. }
  1979. /* global counter which is incremented every time something logs in */
  1980. static atomic_t session_id = ATOMIC_INIT(0);
  1981. /**
  1982. * audit_set_loginuid - set current task's audit_context loginuid
  1983. * @loginuid: loginuid value
  1984. *
  1985. * Returns 0.
  1986. *
  1987. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1988. */
  1989. int audit_set_loginuid(uid_t loginuid)
  1990. {
  1991. struct task_struct *task = current;
  1992. struct audit_context *context = task->audit_context;
  1993. unsigned int sessionid;
  1994. #ifdef CONFIG_AUDIT_LOGINUID_IMMUTABLE
  1995. if (task->loginuid != -1)
  1996. return -EPERM;
  1997. #else /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
  1998. if (!capable(CAP_AUDIT_CONTROL))
  1999. return -EPERM;
  2000. #endif /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
  2001. sessionid = atomic_inc_return(&session_id);
  2002. if (context && context->in_syscall) {
  2003. struct audit_buffer *ab;
  2004. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  2005. if (ab) {
  2006. audit_log_format(ab, "login pid=%d uid=%u "
  2007. "old auid=%u new auid=%u"
  2008. " old ses=%u new ses=%u",
  2009. task->pid, task_uid(task),
  2010. task->loginuid, loginuid,
  2011. task->sessionid, sessionid);
  2012. audit_log_end(ab);
  2013. }
  2014. }
  2015. task->sessionid = sessionid;
  2016. task->loginuid = loginuid;
  2017. return 0;
  2018. }
  2019. /**
  2020. * __audit_mq_open - record audit data for a POSIX MQ open
  2021. * @oflag: open flag
  2022. * @mode: mode bits
  2023. * @attr: queue attributes
  2024. *
  2025. */
  2026. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  2027. {
  2028. struct audit_context *context = current->audit_context;
  2029. if (attr)
  2030. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  2031. else
  2032. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  2033. context->mq_open.oflag = oflag;
  2034. context->mq_open.mode = mode;
  2035. context->type = AUDIT_MQ_OPEN;
  2036. }
  2037. /**
  2038. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  2039. * @mqdes: MQ descriptor
  2040. * @msg_len: Message length
  2041. * @msg_prio: Message priority
  2042. * @abs_timeout: Message timeout in absolute time
  2043. *
  2044. */
  2045. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  2046. const struct timespec *abs_timeout)
  2047. {
  2048. struct audit_context *context = current->audit_context;
  2049. struct timespec *p = &context->mq_sendrecv.abs_timeout;
  2050. if (abs_timeout)
  2051. memcpy(p, abs_timeout, sizeof(struct timespec));
  2052. else
  2053. memset(p, 0, sizeof(struct timespec));
  2054. context->mq_sendrecv.mqdes = mqdes;
  2055. context->mq_sendrecv.msg_len = msg_len;
  2056. context->mq_sendrecv.msg_prio = msg_prio;
  2057. context->type = AUDIT_MQ_SENDRECV;
  2058. }
  2059. /**
  2060. * __audit_mq_notify - record audit data for a POSIX MQ notify
  2061. * @mqdes: MQ descriptor
  2062. * @notification: Notification event
  2063. *
  2064. */
  2065. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  2066. {
  2067. struct audit_context *context = current->audit_context;
  2068. if (notification)
  2069. context->mq_notify.sigev_signo = notification->sigev_signo;
  2070. else
  2071. context->mq_notify.sigev_signo = 0;
  2072. context->mq_notify.mqdes = mqdes;
  2073. context->type = AUDIT_MQ_NOTIFY;
  2074. }
  2075. /**
  2076. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  2077. * @mqdes: MQ descriptor
  2078. * @mqstat: MQ flags
  2079. *
  2080. */
  2081. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  2082. {
  2083. struct audit_context *context = current->audit_context;
  2084. context->mq_getsetattr.mqdes = mqdes;
  2085. context->mq_getsetattr.mqstat = *mqstat;
  2086. context->type = AUDIT_MQ_GETSETATTR;
  2087. }
  2088. /**
  2089. * audit_ipc_obj - record audit data for ipc object
  2090. * @ipcp: ipc permissions
  2091. *
  2092. */
  2093. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  2094. {
  2095. struct audit_context *context = current->audit_context;
  2096. context->ipc.uid = ipcp->uid;
  2097. context->ipc.gid = ipcp->gid;
  2098. context->ipc.mode = ipcp->mode;
  2099. context->ipc.has_perm = 0;
  2100. security_ipc_getsecid(ipcp, &context->ipc.osid);
  2101. context->type = AUDIT_IPC;
  2102. }
  2103. /**
  2104. * audit_ipc_set_perm - record audit data for new ipc permissions
  2105. * @qbytes: msgq bytes
  2106. * @uid: msgq user id
  2107. * @gid: msgq group id
  2108. * @mode: msgq mode (permissions)
  2109. *
  2110. * Called only after audit_ipc_obj().
  2111. */
  2112. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  2113. {
  2114. struct audit_context *context = current->audit_context;
  2115. context->ipc.qbytes = qbytes;
  2116. context->ipc.perm_uid = uid;
  2117. context->ipc.perm_gid = gid;
  2118. context->ipc.perm_mode = mode;
  2119. context->ipc.has_perm = 1;
  2120. }
  2121. int __audit_bprm(struct linux_binprm *bprm)
  2122. {
  2123. struct audit_aux_data_execve *ax;
  2124. struct audit_context *context = current->audit_context;
  2125. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2126. if (!ax)
  2127. return -ENOMEM;
  2128. ax->argc = bprm->argc;
  2129. ax->envc = bprm->envc;
  2130. ax->mm = bprm->mm;
  2131. ax->d.type = AUDIT_EXECVE;
  2132. ax->d.next = context->aux;
  2133. context->aux = (void *)ax;
  2134. return 0;
  2135. }
  2136. /**
  2137. * audit_socketcall - record audit data for sys_socketcall
  2138. * @nargs: number of args
  2139. * @args: args array
  2140. *
  2141. */
  2142. void __audit_socketcall(int nargs, unsigned long *args)
  2143. {
  2144. struct audit_context *context = current->audit_context;
  2145. context->type = AUDIT_SOCKETCALL;
  2146. context->socketcall.nargs = nargs;
  2147. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  2148. }
  2149. /**
  2150. * __audit_fd_pair - record audit data for pipe and socketpair
  2151. * @fd1: the first file descriptor
  2152. * @fd2: the second file descriptor
  2153. *
  2154. */
  2155. void __audit_fd_pair(int fd1, int fd2)
  2156. {
  2157. struct audit_context *context = current->audit_context;
  2158. context->fds[0] = fd1;
  2159. context->fds[1] = fd2;
  2160. }
  2161. /**
  2162. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2163. * @len: data length in user space
  2164. * @a: data address in kernel space
  2165. *
  2166. * Returns 0 for success or NULL context or < 0 on error.
  2167. */
  2168. int __audit_sockaddr(int len, void *a)
  2169. {
  2170. struct audit_context *context = current->audit_context;
  2171. if (!context->sockaddr) {
  2172. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2173. if (!p)
  2174. return -ENOMEM;
  2175. context->sockaddr = p;
  2176. }
  2177. context->sockaddr_len = len;
  2178. memcpy(context->sockaddr, a, len);
  2179. return 0;
  2180. }
  2181. void __audit_ptrace(struct task_struct *t)
  2182. {
  2183. struct audit_context *context = current->audit_context;
  2184. context->target_pid = t->pid;
  2185. context->target_auid = audit_get_loginuid(t);
  2186. context->target_uid = task_uid(t);
  2187. context->target_sessionid = audit_get_sessionid(t);
  2188. security_task_getsecid(t, &context->target_sid);
  2189. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2190. }
  2191. /**
  2192. * audit_signal_info - record signal info for shutting down audit subsystem
  2193. * @sig: signal value
  2194. * @t: task being signaled
  2195. *
  2196. * If the audit subsystem is being terminated, record the task (pid)
  2197. * and uid that is doing that.
  2198. */
  2199. int __audit_signal_info(int sig, struct task_struct *t)
  2200. {
  2201. struct audit_aux_data_pids *axp;
  2202. struct task_struct *tsk = current;
  2203. struct audit_context *ctx = tsk->audit_context;
  2204. uid_t uid = current_uid(), t_uid = task_uid(t);
  2205. if (audit_pid && t->tgid == audit_pid) {
  2206. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2207. audit_sig_pid = tsk->pid;
  2208. if (tsk->loginuid != -1)
  2209. audit_sig_uid = tsk->loginuid;
  2210. else
  2211. audit_sig_uid = uid;
  2212. security_task_getsecid(tsk, &audit_sig_sid);
  2213. }
  2214. if (!audit_signals || audit_dummy_context())
  2215. return 0;
  2216. }
  2217. /* optimize the common case by putting first signal recipient directly
  2218. * in audit_context */
  2219. if (!ctx->target_pid) {
  2220. ctx->target_pid = t->tgid;
  2221. ctx->target_auid = audit_get_loginuid(t);
  2222. ctx->target_uid = t_uid;
  2223. ctx->target_sessionid = audit_get_sessionid(t);
  2224. security_task_getsecid(t, &ctx->target_sid);
  2225. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2226. return 0;
  2227. }
  2228. axp = (void *)ctx->aux_pids;
  2229. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2230. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2231. if (!axp)
  2232. return -ENOMEM;
  2233. axp->d.type = AUDIT_OBJ_PID;
  2234. axp->d.next = ctx->aux_pids;
  2235. ctx->aux_pids = (void *)axp;
  2236. }
  2237. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2238. axp->target_pid[axp->pid_count] = t->tgid;
  2239. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2240. axp->target_uid[axp->pid_count] = t_uid;
  2241. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2242. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2243. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2244. axp->pid_count++;
  2245. return 0;
  2246. }
  2247. /**
  2248. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2249. * @bprm: pointer to the bprm being processed
  2250. * @new: the proposed new credentials
  2251. * @old: the old credentials
  2252. *
  2253. * Simply check if the proc already has the caps given by the file and if not
  2254. * store the priv escalation info for later auditing at the end of the syscall
  2255. *
  2256. * -Eric
  2257. */
  2258. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2259. const struct cred *new, const struct cred *old)
  2260. {
  2261. struct audit_aux_data_bprm_fcaps *ax;
  2262. struct audit_context *context = current->audit_context;
  2263. struct cpu_vfs_cap_data vcaps;
  2264. struct dentry *dentry;
  2265. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2266. if (!ax)
  2267. return -ENOMEM;
  2268. ax->d.type = AUDIT_BPRM_FCAPS;
  2269. ax->d.next = context->aux;
  2270. context->aux = (void *)ax;
  2271. dentry = dget(bprm->file->f_dentry);
  2272. get_vfs_caps_from_disk(dentry, &vcaps);
  2273. dput(dentry);
  2274. ax->fcap.permitted = vcaps.permitted;
  2275. ax->fcap.inheritable = vcaps.inheritable;
  2276. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2277. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2278. ax->old_pcap.permitted = old->cap_permitted;
  2279. ax->old_pcap.inheritable = old->cap_inheritable;
  2280. ax->old_pcap.effective = old->cap_effective;
  2281. ax->new_pcap.permitted = new->cap_permitted;
  2282. ax->new_pcap.inheritable = new->cap_inheritable;
  2283. ax->new_pcap.effective = new->cap_effective;
  2284. return 0;
  2285. }
  2286. /**
  2287. * __audit_log_capset - store information about the arguments to the capset syscall
  2288. * @pid: target pid of the capset call
  2289. * @new: the new credentials
  2290. * @old: the old (current) credentials
  2291. *
  2292. * Record the aguments userspace sent to sys_capset for later printing by the
  2293. * audit system if applicable
  2294. */
  2295. void __audit_log_capset(pid_t pid,
  2296. const struct cred *new, const struct cred *old)
  2297. {
  2298. struct audit_context *context = current->audit_context;
  2299. context->capset.pid = pid;
  2300. context->capset.cap.effective = new->cap_effective;
  2301. context->capset.cap.inheritable = new->cap_effective;
  2302. context->capset.cap.permitted = new->cap_permitted;
  2303. context->type = AUDIT_CAPSET;
  2304. }
  2305. void __audit_mmap_fd(int fd, int flags)
  2306. {
  2307. struct audit_context *context = current->audit_context;
  2308. context->mmap.fd = fd;
  2309. context->mmap.flags = flags;
  2310. context->type = AUDIT_MMAP;
  2311. }
  2312. static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
  2313. {
  2314. uid_t auid, uid;
  2315. gid_t gid;
  2316. unsigned int sessionid;
  2317. auid = audit_get_loginuid(current);
  2318. sessionid = audit_get_sessionid(current);
  2319. current_uid_gid(&uid, &gid);
  2320. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2321. auid, uid, gid, sessionid);
  2322. audit_log_task_context(ab);
  2323. audit_log_format(ab, " pid=%d comm=", current->pid);
  2324. audit_log_untrustedstring(ab, current->comm);
  2325. audit_log_format(ab, " reason=");
  2326. audit_log_string(ab, reason);
  2327. audit_log_format(ab, " sig=%ld", signr);
  2328. }
  2329. /**
  2330. * audit_core_dumps - record information about processes that end abnormally
  2331. * @signr: signal value
  2332. *
  2333. * If a process ends with a core dump, something fishy is going on and we
  2334. * should record the event for investigation.
  2335. */
  2336. void audit_core_dumps(long signr)
  2337. {
  2338. struct audit_buffer *ab;
  2339. if (!audit_enabled)
  2340. return;
  2341. if (signr == SIGQUIT) /* don't care for those */
  2342. return;
  2343. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2344. audit_log_abend(ab, "memory violation", signr);
  2345. audit_log_end(ab);
  2346. }
  2347. void __audit_seccomp(unsigned long syscall)
  2348. {
  2349. struct audit_buffer *ab;
  2350. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2351. audit_log_abend(ab, "seccomp", SIGKILL);
  2352. audit_log_format(ab, " syscall=%ld", syscall);
  2353. audit_log_end(ab);
  2354. }
  2355. struct list_head *audit_killed_trees(void)
  2356. {
  2357. struct audit_context *ctx = current->audit_context;
  2358. if (likely(!ctx || !ctx->in_syscall))
  2359. return NULL;
  2360. return &ctx->killed_trees;
  2361. }