auditsc.c 71 KB

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