auditsc.c 72 KB

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