auditsc.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  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 void audit_zero_context(struct audit_context *context,
  963. enum audit_state state)
  964. {
  965. memset(context, 0, sizeof(*context));
  966. context->state = state;
  967. context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  968. }
  969. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  970. {
  971. struct audit_context *context;
  972. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  973. return NULL;
  974. audit_zero_context(context, state);
  975. INIT_LIST_HEAD(&context->killed_trees);
  976. INIT_LIST_HEAD(&context->names_list);
  977. return context;
  978. }
  979. /**
  980. * audit_alloc - allocate an audit context block for a task
  981. * @tsk: task
  982. *
  983. * Filter on the task information and allocate a per-task audit context
  984. * if necessary. Doing so turns on system call auditing for the
  985. * specified task. This is called from copy_process, so no lock is
  986. * needed.
  987. */
  988. int audit_alloc(struct task_struct *tsk)
  989. {
  990. struct audit_context *context;
  991. enum audit_state state;
  992. char *key = NULL;
  993. if (likely(!audit_ever_enabled))
  994. return 0; /* Return if not auditing. */
  995. state = audit_filter_task(tsk, &key);
  996. if (state == AUDIT_DISABLED)
  997. return 0;
  998. if (!(context = audit_alloc_context(state))) {
  999. kfree(key);
  1000. audit_log_lost("out of memory in audit_alloc");
  1001. return -ENOMEM;
  1002. }
  1003. context->filterkey = key;
  1004. tsk->audit_context = context;
  1005. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  1006. return 0;
  1007. }
  1008. static inline void audit_free_context(struct audit_context *context)
  1009. {
  1010. audit_free_names(context);
  1011. unroll_tree_refs(context, NULL, 0);
  1012. free_tree_refs(context);
  1013. audit_free_aux(context);
  1014. kfree(context->filterkey);
  1015. kfree(context->sockaddr);
  1016. kfree(context);
  1017. }
  1018. void audit_log_task_context(struct audit_buffer *ab)
  1019. {
  1020. char *ctx = NULL;
  1021. unsigned len;
  1022. int error;
  1023. u32 sid;
  1024. security_task_getsecid(current, &sid);
  1025. if (!sid)
  1026. return;
  1027. error = security_secid_to_secctx(sid, &ctx, &len);
  1028. if (error) {
  1029. if (error != -EINVAL)
  1030. goto error_path;
  1031. return;
  1032. }
  1033. audit_log_format(ab, " subj=%s", ctx);
  1034. security_release_secctx(ctx, len);
  1035. return;
  1036. error_path:
  1037. audit_panic("error in audit_log_task_context");
  1038. return;
  1039. }
  1040. EXPORT_SYMBOL(audit_log_task_context);
  1041. void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  1042. {
  1043. const struct cred *cred;
  1044. char name[sizeof(tsk->comm)];
  1045. struct mm_struct *mm = tsk->mm;
  1046. char *tty;
  1047. if (!ab)
  1048. return;
  1049. /* tsk == current */
  1050. cred = current_cred();
  1051. spin_lock_irq(&tsk->sighand->siglock);
  1052. if (tsk->signal && tsk->signal->tty)
  1053. tty = tsk->signal->tty->name;
  1054. else
  1055. tty = "(none)";
  1056. spin_unlock_irq(&tsk->sighand->siglock);
  1057. audit_log_format(ab,
  1058. " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
  1059. " euid=%u suid=%u fsuid=%u"
  1060. " egid=%u sgid=%u fsgid=%u ses=%u tty=%s",
  1061. sys_getppid(),
  1062. tsk->pid,
  1063. from_kuid(&init_user_ns, tsk->loginuid),
  1064. from_kuid(&init_user_ns, cred->uid),
  1065. from_kgid(&init_user_ns, cred->gid),
  1066. from_kuid(&init_user_ns, cred->euid),
  1067. from_kuid(&init_user_ns, cred->suid),
  1068. from_kuid(&init_user_ns, cred->fsuid),
  1069. from_kgid(&init_user_ns, cred->egid),
  1070. from_kgid(&init_user_ns, cred->sgid),
  1071. from_kgid(&init_user_ns, cred->fsgid),
  1072. tsk->sessionid, tty);
  1073. get_task_comm(name, tsk);
  1074. audit_log_format(ab, " comm=");
  1075. audit_log_untrustedstring(ab, name);
  1076. if (mm) {
  1077. down_read(&mm->mmap_sem);
  1078. if (mm->exe_file)
  1079. audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
  1080. up_read(&mm->mmap_sem);
  1081. }
  1082. audit_log_task_context(ab);
  1083. }
  1084. EXPORT_SYMBOL(audit_log_task_info);
  1085. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  1086. kuid_t auid, kuid_t uid, unsigned int sessionid,
  1087. u32 sid, char *comm)
  1088. {
  1089. struct audit_buffer *ab;
  1090. char *ctx = NULL;
  1091. u32 len;
  1092. int rc = 0;
  1093. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  1094. if (!ab)
  1095. return rc;
  1096. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
  1097. from_kuid(&init_user_ns, auid),
  1098. from_kuid(&init_user_ns, uid), sessionid);
  1099. if (security_secid_to_secctx(sid, &ctx, &len)) {
  1100. audit_log_format(ab, " obj=(none)");
  1101. rc = 1;
  1102. } else {
  1103. audit_log_format(ab, " obj=%s", ctx);
  1104. security_release_secctx(ctx, len);
  1105. }
  1106. audit_log_format(ab, " ocomm=");
  1107. audit_log_untrustedstring(ab, comm);
  1108. audit_log_end(ab);
  1109. return rc;
  1110. }
  1111. /*
  1112. * to_send and len_sent accounting are very loose estimates. We aren't
  1113. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  1114. * within about 500 bytes (next page boundary)
  1115. *
  1116. * why snprintf? an int is up to 12 digits long. if we just assumed when
  1117. * logging that a[%d]= was going to be 16 characters long we would be wasting
  1118. * space in every audit message. In one 7500 byte message we can log up to
  1119. * about 1000 min size arguments. That comes down to about 50% waste of space
  1120. * if we didn't do the snprintf to find out how long arg_num_len was.
  1121. */
  1122. static int audit_log_single_execve_arg(struct audit_context *context,
  1123. struct audit_buffer **ab,
  1124. int arg_num,
  1125. size_t *len_sent,
  1126. const char __user *p,
  1127. char *buf)
  1128. {
  1129. char arg_num_len_buf[12];
  1130. const char __user *tmp_p = p;
  1131. /* how many digits are in arg_num? 5 is the length of ' a=""' */
  1132. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
  1133. size_t len, len_left, to_send;
  1134. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  1135. unsigned int i, has_cntl = 0, too_long = 0;
  1136. int ret;
  1137. /* strnlen_user includes the null we don't want to send */
  1138. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  1139. /*
  1140. * We just created this mm, if we can't find the strings
  1141. * we just copied into it something is _very_ wrong. Similar
  1142. * for strings that are too long, we should not have created
  1143. * any.
  1144. */
  1145. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  1146. WARN_ON(1);
  1147. send_sig(SIGKILL, current, 0);
  1148. return -1;
  1149. }
  1150. /* walk the whole argument looking for non-ascii chars */
  1151. do {
  1152. if (len_left > MAX_EXECVE_AUDIT_LEN)
  1153. to_send = MAX_EXECVE_AUDIT_LEN;
  1154. else
  1155. to_send = len_left;
  1156. ret = copy_from_user(buf, tmp_p, to_send);
  1157. /*
  1158. * There is no reason for this copy to be short. We just
  1159. * copied them here, and the mm hasn't been exposed to user-
  1160. * space yet.
  1161. */
  1162. if (ret) {
  1163. WARN_ON(1);
  1164. send_sig(SIGKILL, current, 0);
  1165. return -1;
  1166. }
  1167. buf[to_send] = '\0';
  1168. has_cntl = audit_string_contains_control(buf, to_send);
  1169. if (has_cntl) {
  1170. /*
  1171. * hex messages get logged as 2 bytes, so we can only
  1172. * send half as much in each message
  1173. */
  1174. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  1175. break;
  1176. }
  1177. len_left -= to_send;
  1178. tmp_p += to_send;
  1179. } while (len_left > 0);
  1180. len_left = len;
  1181. if (len > max_execve_audit_len)
  1182. too_long = 1;
  1183. /* rewalk the argument actually logging the message */
  1184. for (i = 0; len_left > 0; i++) {
  1185. int room_left;
  1186. if (len_left > max_execve_audit_len)
  1187. to_send = max_execve_audit_len;
  1188. else
  1189. to_send = len_left;
  1190. /* do we have space left to send this argument in this ab? */
  1191. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  1192. if (has_cntl)
  1193. room_left -= (to_send * 2);
  1194. else
  1195. room_left -= to_send;
  1196. if (room_left < 0) {
  1197. *len_sent = 0;
  1198. audit_log_end(*ab);
  1199. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  1200. if (!*ab)
  1201. return 0;
  1202. }
  1203. /*
  1204. * first record needs to say how long the original string was
  1205. * so we can be sure nothing was lost.
  1206. */
  1207. if ((i == 0) && (too_long))
  1208. audit_log_format(*ab, " a%d_len=%zu", arg_num,
  1209. has_cntl ? 2*len : len);
  1210. /*
  1211. * normally arguments are small enough to fit and we already
  1212. * filled buf above when we checked for control characters
  1213. * so don't bother with another copy_from_user
  1214. */
  1215. if (len >= max_execve_audit_len)
  1216. ret = copy_from_user(buf, p, to_send);
  1217. else
  1218. ret = 0;
  1219. if (ret) {
  1220. WARN_ON(1);
  1221. send_sig(SIGKILL, current, 0);
  1222. return -1;
  1223. }
  1224. buf[to_send] = '\0';
  1225. /* actually log it */
  1226. audit_log_format(*ab, " a%d", arg_num);
  1227. if (too_long)
  1228. audit_log_format(*ab, "[%d]", i);
  1229. audit_log_format(*ab, "=");
  1230. if (has_cntl)
  1231. audit_log_n_hex(*ab, buf, to_send);
  1232. else
  1233. audit_log_string(*ab, buf);
  1234. p += to_send;
  1235. len_left -= to_send;
  1236. *len_sent += arg_num_len;
  1237. if (has_cntl)
  1238. *len_sent += to_send * 2;
  1239. else
  1240. *len_sent += to_send;
  1241. }
  1242. /* include the null we didn't log */
  1243. return len + 1;
  1244. }
  1245. static void audit_log_execve_info(struct audit_context *context,
  1246. struct audit_buffer **ab,
  1247. struct audit_aux_data_execve *axi)
  1248. {
  1249. int i, len;
  1250. size_t len_sent = 0;
  1251. const char __user *p;
  1252. char *buf;
  1253. if (axi->mm != current->mm)
  1254. return; /* execve failed, no additional info */
  1255. p = (const char __user *)axi->mm->arg_start;
  1256. audit_log_format(*ab, "argc=%d", axi->argc);
  1257. /*
  1258. * we need some kernel buffer to hold the userspace args. Just
  1259. * allocate one big one rather than allocating one of the right size
  1260. * for every single argument inside audit_log_single_execve_arg()
  1261. * should be <8k allocation so should be pretty safe.
  1262. */
  1263. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1264. if (!buf) {
  1265. audit_panic("out of memory for argv string\n");
  1266. return;
  1267. }
  1268. for (i = 0; i < axi->argc; i++) {
  1269. len = audit_log_single_execve_arg(context, ab, i,
  1270. &len_sent, p, buf);
  1271. if (len <= 0)
  1272. break;
  1273. p += len;
  1274. }
  1275. kfree(buf);
  1276. }
  1277. static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
  1278. {
  1279. int i;
  1280. audit_log_format(ab, " %s=", prefix);
  1281. CAP_FOR_EACH_U32(i) {
  1282. audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
  1283. }
  1284. }
  1285. static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
  1286. {
  1287. kernel_cap_t *perm = &name->fcap.permitted;
  1288. kernel_cap_t *inh = &name->fcap.inheritable;
  1289. int log = 0;
  1290. if (!cap_isclear(*perm)) {
  1291. audit_log_cap(ab, "cap_fp", perm);
  1292. log = 1;
  1293. }
  1294. if (!cap_isclear(*inh)) {
  1295. audit_log_cap(ab, "cap_fi", inh);
  1296. log = 1;
  1297. }
  1298. if (log)
  1299. audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
  1300. }
  1301. static void show_special(struct audit_context *context, int *call_panic)
  1302. {
  1303. struct audit_buffer *ab;
  1304. int i;
  1305. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1306. if (!ab)
  1307. return;
  1308. switch (context->type) {
  1309. case AUDIT_SOCKETCALL: {
  1310. int nargs = context->socketcall.nargs;
  1311. audit_log_format(ab, "nargs=%d", nargs);
  1312. for (i = 0; i < nargs; i++)
  1313. audit_log_format(ab, " a%d=%lx", i,
  1314. context->socketcall.args[i]);
  1315. break; }
  1316. case AUDIT_IPC: {
  1317. u32 osid = context->ipc.osid;
  1318. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1319. from_kuid(&init_user_ns, context->ipc.uid),
  1320. from_kgid(&init_user_ns, context->ipc.gid),
  1321. context->ipc.mode);
  1322. if (osid) {
  1323. char *ctx = NULL;
  1324. u32 len;
  1325. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1326. audit_log_format(ab, " osid=%u", osid);
  1327. *call_panic = 1;
  1328. } else {
  1329. audit_log_format(ab, " obj=%s", ctx);
  1330. security_release_secctx(ctx, len);
  1331. }
  1332. }
  1333. if (context->ipc.has_perm) {
  1334. audit_log_end(ab);
  1335. ab = audit_log_start(context, GFP_KERNEL,
  1336. AUDIT_IPC_SET_PERM);
  1337. if (unlikely(!ab))
  1338. return;
  1339. audit_log_format(ab,
  1340. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1341. context->ipc.qbytes,
  1342. context->ipc.perm_uid,
  1343. context->ipc.perm_gid,
  1344. context->ipc.perm_mode);
  1345. }
  1346. break; }
  1347. case AUDIT_MQ_OPEN: {
  1348. audit_log_format(ab,
  1349. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1350. "mq_msgsize=%ld mq_curmsgs=%ld",
  1351. context->mq_open.oflag, context->mq_open.mode,
  1352. context->mq_open.attr.mq_flags,
  1353. context->mq_open.attr.mq_maxmsg,
  1354. context->mq_open.attr.mq_msgsize,
  1355. context->mq_open.attr.mq_curmsgs);
  1356. break; }
  1357. case AUDIT_MQ_SENDRECV: {
  1358. audit_log_format(ab,
  1359. "mqdes=%d msg_len=%zd msg_prio=%u "
  1360. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1361. context->mq_sendrecv.mqdes,
  1362. context->mq_sendrecv.msg_len,
  1363. context->mq_sendrecv.msg_prio,
  1364. context->mq_sendrecv.abs_timeout.tv_sec,
  1365. context->mq_sendrecv.abs_timeout.tv_nsec);
  1366. break; }
  1367. case AUDIT_MQ_NOTIFY: {
  1368. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1369. context->mq_notify.mqdes,
  1370. context->mq_notify.sigev_signo);
  1371. break; }
  1372. case AUDIT_MQ_GETSETATTR: {
  1373. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1374. audit_log_format(ab,
  1375. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1376. "mq_curmsgs=%ld ",
  1377. context->mq_getsetattr.mqdes,
  1378. attr->mq_flags, attr->mq_maxmsg,
  1379. attr->mq_msgsize, attr->mq_curmsgs);
  1380. break; }
  1381. case AUDIT_CAPSET: {
  1382. audit_log_format(ab, "pid=%d", context->capset.pid);
  1383. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1384. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1385. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1386. break; }
  1387. case AUDIT_MMAP: {
  1388. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1389. context->mmap.flags);
  1390. break; }
  1391. }
  1392. audit_log_end(ab);
  1393. }
  1394. static void audit_log_name(struct audit_context *context, struct audit_names *n,
  1395. int record_num, int *call_panic)
  1396. {
  1397. struct audit_buffer *ab;
  1398. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1399. if (!ab)
  1400. return; /* audit_panic has been called */
  1401. audit_log_format(ab, "item=%d", record_num);
  1402. if (n->name) {
  1403. switch (n->name_len) {
  1404. case AUDIT_NAME_FULL:
  1405. /* log the full path */
  1406. audit_log_format(ab, " name=");
  1407. audit_log_untrustedstring(ab, n->name->name);
  1408. break;
  1409. case 0:
  1410. /* name was specified as a relative path and the
  1411. * directory component is the cwd */
  1412. audit_log_d_path(ab, " name=", &context->pwd);
  1413. break;
  1414. default:
  1415. /* log the name's directory component */
  1416. audit_log_format(ab, " name=");
  1417. audit_log_n_untrustedstring(ab, n->name->name,
  1418. n->name_len);
  1419. }
  1420. } else
  1421. audit_log_format(ab, " name=(null)");
  1422. if (n->ino != (unsigned long)-1) {
  1423. audit_log_format(ab, " inode=%lu"
  1424. " dev=%02x:%02x mode=%#ho"
  1425. " ouid=%u ogid=%u rdev=%02x:%02x",
  1426. n->ino,
  1427. MAJOR(n->dev),
  1428. MINOR(n->dev),
  1429. n->mode,
  1430. from_kuid(&init_user_ns, n->uid),
  1431. from_kgid(&init_user_ns, n->gid),
  1432. MAJOR(n->rdev),
  1433. MINOR(n->rdev));
  1434. }
  1435. if (n->osid != 0) {
  1436. char *ctx = NULL;
  1437. u32 len;
  1438. if (security_secid_to_secctx(
  1439. n->osid, &ctx, &len)) {
  1440. audit_log_format(ab, " osid=%u", n->osid);
  1441. *call_panic = 2;
  1442. } else {
  1443. audit_log_format(ab, " obj=%s", ctx);
  1444. security_release_secctx(ctx, len);
  1445. }
  1446. }
  1447. audit_log_fcaps(ab, n);
  1448. audit_log_end(ab);
  1449. }
  1450. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1451. {
  1452. int i, call_panic = 0;
  1453. struct audit_buffer *ab;
  1454. struct audit_aux_data *aux;
  1455. struct audit_names *n;
  1456. /* tsk == current */
  1457. context->personality = tsk->personality;
  1458. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1459. if (!ab)
  1460. return; /* audit_panic has been called */
  1461. audit_log_format(ab, "arch=%x syscall=%d",
  1462. context->arch, context->major);
  1463. if (context->personality != PER_LINUX)
  1464. audit_log_format(ab, " per=%lx", context->personality);
  1465. if (context->return_valid)
  1466. audit_log_format(ab, " success=%s exit=%ld",
  1467. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1468. context->return_code);
  1469. audit_log_format(ab,
  1470. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
  1471. context->argv[0],
  1472. context->argv[1],
  1473. context->argv[2],
  1474. context->argv[3],
  1475. context->name_count);
  1476. audit_log_task_info(ab, tsk);
  1477. audit_log_key(ab, context->filterkey);
  1478. audit_log_end(ab);
  1479. for (aux = context->aux; aux; aux = aux->next) {
  1480. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1481. if (!ab)
  1482. continue; /* audit_panic has been called */
  1483. switch (aux->type) {
  1484. case AUDIT_EXECVE: {
  1485. struct audit_aux_data_execve *axi = (void *)aux;
  1486. audit_log_execve_info(context, &ab, axi);
  1487. break; }
  1488. case AUDIT_BPRM_FCAPS: {
  1489. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1490. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1491. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1492. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1493. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1494. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1495. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1496. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1497. audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
  1498. audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
  1499. audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
  1500. break; }
  1501. }
  1502. audit_log_end(ab);
  1503. }
  1504. if (context->type)
  1505. show_special(context, &call_panic);
  1506. if (context->fds[0] >= 0) {
  1507. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1508. if (ab) {
  1509. audit_log_format(ab, "fd0=%d fd1=%d",
  1510. context->fds[0], context->fds[1]);
  1511. audit_log_end(ab);
  1512. }
  1513. }
  1514. if (context->sockaddr_len) {
  1515. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1516. if (ab) {
  1517. audit_log_format(ab, "saddr=");
  1518. audit_log_n_hex(ab, (void *)context->sockaddr,
  1519. context->sockaddr_len);
  1520. audit_log_end(ab);
  1521. }
  1522. }
  1523. for (aux = context->aux_pids; aux; aux = aux->next) {
  1524. struct audit_aux_data_pids *axs = (void *)aux;
  1525. for (i = 0; i < axs->pid_count; i++)
  1526. if (audit_log_pid_context(context, axs->target_pid[i],
  1527. axs->target_auid[i],
  1528. axs->target_uid[i],
  1529. axs->target_sessionid[i],
  1530. axs->target_sid[i],
  1531. axs->target_comm[i]))
  1532. call_panic = 1;
  1533. }
  1534. if (context->target_pid &&
  1535. audit_log_pid_context(context, context->target_pid,
  1536. context->target_auid, context->target_uid,
  1537. context->target_sessionid,
  1538. context->target_sid, context->target_comm))
  1539. call_panic = 1;
  1540. if (context->pwd.dentry && context->pwd.mnt) {
  1541. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1542. if (ab) {
  1543. audit_log_d_path(ab, " cwd=", &context->pwd);
  1544. audit_log_end(ab);
  1545. }
  1546. }
  1547. i = 0;
  1548. list_for_each_entry(n, &context->names_list, list)
  1549. audit_log_name(context, n, i++, &call_panic);
  1550. /* Send end of event record to help user space know we are finished */
  1551. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1552. if (ab)
  1553. audit_log_end(ab);
  1554. if (call_panic)
  1555. audit_panic("error converting sid to string");
  1556. }
  1557. /**
  1558. * audit_free - free a per-task audit context
  1559. * @tsk: task whose audit context block to free
  1560. *
  1561. * Called from copy_process and do_exit
  1562. */
  1563. void __audit_free(struct task_struct *tsk)
  1564. {
  1565. struct audit_context *context;
  1566. context = audit_get_context(tsk, 0, 0);
  1567. if (!context)
  1568. return;
  1569. /* Check for system calls that do not go through the exit
  1570. * function (e.g., exit_group), then free context block.
  1571. * We use GFP_ATOMIC here because we might be doing this
  1572. * in the context of the idle thread */
  1573. /* that can happen only if we are called from do_exit() */
  1574. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1575. audit_log_exit(context, tsk);
  1576. if (!list_empty(&context->killed_trees))
  1577. audit_kill_trees(&context->killed_trees);
  1578. audit_free_context(context);
  1579. }
  1580. /**
  1581. * audit_syscall_entry - fill in an audit record at syscall entry
  1582. * @arch: architecture type
  1583. * @major: major syscall type (function)
  1584. * @a1: additional syscall register 1
  1585. * @a2: additional syscall register 2
  1586. * @a3: additional syscall register 3
  1587. * @a4: additional syscall register 4
  1588. *
  1589. * Fill in audit context at syscall entry. This only happens if the
  1590. * audit context was created when the task was created and the state or
  1591. * filters demand the audit context be built. If the state from the
  1592. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1593. * then the record will be written at syscall exit time (otherwise, it
  1594. * will only be written if another part of the kernel requests that it
  1595. * be written).
  1596. */
  1597. void __audit_syscall_entry(int arch, int major,
  1598. unsigned long a1, unsigned long a2,
  1599. unsigned long a3, unsigned long a4)
  1600. {
  1601. struct task_struct *tsk = current;
  1602. struct audit_context *context = tsk->audit_context;
  1603. enum audit_state state;
  1604. if (!context)
  1605. return;
  1606. BUG_ON(context->in_syscall || context->name_count);
  1607. if (!audit_enabled)
  1608. return;
  1609. context->arch = arch;
  1610. context->major = major;
  1611. context->argv[0] = a1;
  1612. context->argv[1] = a2;
  1613. context->argv[2] = a3;
  1614. context->argv[3] = a4;
  1615. state = context->state;
  1616. context->dummy = !audit_n_rules;
  1617. if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
  1618. context->prio = 0;
  1619. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1620. }
  1621. if (state == AUDIT_DISABLED)
  1622. return;
  1623. context->serial = 0;
  1624. context->ctime = CURRENT_TIME;
  1625. context->in_syscall = 1;
  1626. context->current_state = state;
  1627. context->ppid = 0;
  1628. }
  1629. /**
  1630. * audit_syscall_exit - deallocate audit context after a system call
  1631. * @success: success value of the syscall
  1632. * @return_code: return value of the syscall
  1633. *
  1634. * Tear down after system call. If the audit context has been marked as
  1635. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1636. * filtering, or because some other part of the kernel wrote an audit
  1637. * message), then write out the syscall information. In call cases,
  1638. * free the names stored from getname().
  1639. */
  1640. void __audit_syscall_exit(int success, long return_code)
  1641. {
  1642. struct task_struct *tsk = current;
  1643. struct audit_context *context;
  1644. if (success)
  1645. success = AUDITSC_SUCCESS;
  1646. else
  1647. success = AUDITSC_FAILURE;
  1648. context = audit_get_context(tsk, success, return_code);
  1649. if (!context)
  1650. return;
  1651. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1652. audit_log_exit(context, tsk);
  1653. context->in_syscall = 0;
  1654. context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  1655. if (!list_empty(&context->killed_trees))
  1656. audit_kill_trees(&context->killed_trees);
  1657. audit_free_names(context);
  1658. unroll_tree_refs(context, NULL, 0);
  1659. audit_free_aux(context);
  1660. context->aux = NULL;
  1661. context->aux_pids = NULL;
  1662. context->target_pid = 0;
  1663. context->target_sid = 0;
  1664. context->sockaddr_len = 0;
  1665. context->type = 0;
  1666. context->fds[0] = -1;
  1667. if (context->state != AUDIT_RECORD_CONTEXT) {
  1668. kfree(context->filterkey);
  1669. context->filterkey = NULL;
  1670. }
  1671. tsk->audit_context = context;
  1672. }
  1673. static inline void handle_one(const struct inode *inode)
  1674. {
  1675. #ifdef CONFIG_AUDIT_TREE
  1676. struct audit_context *context;
  1677. struct audit_tree_refs *p;
  1678. struct audit_chunk *chunk;
  1679. int count;
  1680. if (likely(hlist_empty(&inode->i_fsnotify_marks)))
  1681. return;
  1682. context = current->audit_context;
  1683. p = context->trees;
  1684. count = context->tree_count;
  1685. rcu_read_lock();
  1686. chunk = audit_tree_lookup(inode);
  1687. rcu_read_unlock();
  1688. if (!chunk)
  1689. return;
  1690. if (likely(put_tree_ref(context, chunk)))
  1691. return;
  1692. if (unlikely(!grow_tree_refs(context))) {
  1693. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1694. audit_set_auditable(context);
  1695. audit_put_chunk(chunk);
  1696. unroll_tree_refs(context, p, count);
  1697. return;
  1698. }
  1699. put_tree_ref(context, chunk);
  1700. #endif
  1701. }
  1702. static void handle_path(const struct dentry *dentry)
  1703. {
  1704. #ifdef CONFIG_AUDIT_TREE
  1705. struct audit_context *context;
  1706. struct audit_tree_refs *p;
  1707. const struct dentry *d, *parent;
  1708. struct audit_chunk *drop;
  1709. unsigned long seq;
  1710. int count;
  1711. context = current->audit_context;
  1712. p = context->trees;
  1713. count = context->tree_count;
  1714. retry:
  1715. drop = NULL;
  1716. d = dentry;
  1717. rcu_read_lock();
  1718. seq = read_seqbegin(&rename_lock);
  1719. for(;;) {
  1720. struct inode *inode = d->d_inode;
  1721. if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
  1722. struct audit_chunk *chunk;
  1723. chunk = audit_tree_lookup(inode);
  1724. if (chunk) {
  1725. if (unlikely(!put_tree_ref(context, chunk))) {
  1726. drop = chunk;
  1727. break;
  1728. }
  1729. }
  1730. }
  1731. parent = d->d_parent;
  1732. if (parent == d)
  1733. break;
  1734. d = parent;
  1735. }
  1736. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1737. rcu_read_unlock();
  1738. if (!drop) {
  1739. /* just a race with rename */
  1740. unroll_tree_refs(context, p, count);
  1741. goto retry;
  1742. }
  1743. audit_put_chunk(drop);
  1744. if (grow_tree_refs(context)) {
  1745. /* OK, got more space */
  1746. unroll_tree_refs(context, p, count);
  1747. goto retry;
  1748. }
  1749. /* too bad */
  1750. printk(KERN_WARNING
  1751. "out of memory, audit has lost a tree reference\n");
  1752. unroll_tree_refs(context, p, count);
  1753. audit_set_auditable(context);
  1754. return;
  1755. }
  1756. rcu_read_unlock();
  1757. #endif
  1758. }
  1759. static struct audit_names *audit_alloc_name(struct audit_context *context,
  1760. unsigned char type)
  1761. {
  1762. struct audit_names *aname;
  1763. if (context->name_count < AUDIT_NAMES) {
  1764. aname = &context->preallocated_names[context->name_count];
  1765. memset(aname, 0, sizeof(*aname));
  1766. } else {
  1767. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1768. if (!aname)
  1769. return NULL;
  1770. aname->should_free = true;
  1771. }
  1772. aname->ino = (unsigned long)-1;
  1773. aname->type = type;
  1774. list_add_tail(&aname->list, &context->names_list);
  1775. context->name_count++;
  1776. #if AUDIT_DEBUG
  1777. context->ino_count++;
  1778. #endif
  1779. return aname;
  1780. }
  1781. /**
  1782. * audit_reusename - fill out filename with info from existing entry
  1783. * @uptr: userland ptr to pathname
  1784. *
  1785. * Search the audit_names list for the current audit context. If there is an
  1786. * existing entry with a matching "uptr" then return the filename
  1787. * associated with that audit_name. If not, return NULL.
  1788. */
  1789. struct filename *
  1790. __audit_reusename(const __user char *uptr)
  1791. {
  1792. struct audit_context *context = current->audit_context;
  1793. struct audit_names *n;
  1794. list_for_each_entry(n, &context->names_list, list) {
  1795. if (!n->name)
  1796. continue;
  1797. if (n->name->uptr == uptr)
  1798. return n->name;
  1799. }
  1800. return NULL;
  1801. }
  1802. /**
  1803. * audit_getname - add a name to the list
  1804. * @name: name to add
  1805. *
  1806. * Add a name to the list of audit names for this context.
  1807. * Called from fs/namei.c:getname().
  1808. */
  1809. void __audit_getname(struct filename *name)
  1810. {
  1811. struct audit_context *context = current->audit_context;
  1812. struct audit_names *n;
  1813. if (!context->in_syscall) {
  1814. #if AUDIT_DEBUG == 2
  1815. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1816. __FILE__, __LINE__, context->serial, name);
  1817. dump_stack();
  1818. #endif
  1819. return;
  1820. }
  1821. #if AUDIT_DEBUG
  1822. /* The filename _must_ have a populated ->name */
  1823. BUG_ON(!name->name);
  1824. #endif
  1825. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  1826. if (!n)
  1827. return;
  1828. n->name = name;
  1829. n->name_len = AUDIT_NAME_FULL;
  1830. n->name_put = true;
  1831. name->aname = n;
  1832. if (!context->pwd.dentry)
  1833. get_fs_pwd(current->fs, &context->pwd);
  1834. }
  1835. /* audit_putname - intercept a putname request
  1836. * @name: name to intercept and delay for putname
  1837. *
  1838. * If we have stored the name from getname in the audit context,
  1839. * then we delay the putname until syscall exit.
  1840. * Called from include/linux/fs.h:putname().
  1841. */
  1842. void audit_putname(struct filename *name)
  1843. {
  1844. struct audit_context *context = current->audit_context;
  1845. BUG_ON(!context);
  1846. if (!context->in_syscall) {
  1847. #if AUDIT_DEBUG == 2
  1848. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1849. __FILE__, __LINE__, context->serial, name);
  1850. if (context->name_count) {
  1851. struct audit_names *n;
  1852. int i;
  1853. list_for_each_entry(n, &context->names_list, list)
  1854. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1855. n->name, n->name->name ?: "(null)");
  1856. }
  1857. #endif
  1858. __putname(name);
  1859. }
  1860. #if AUDIT_DEBUG
  1861. else {
  1862. ++context->put_count;
  1863. if (context->put_count > context->name_count) {
  1864. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1865. " in_syscall=%d putname(%p) name_count=%d"
  1866. " put_count=%d\n",
  1867. __FILE__, __LINE__,
  1868. context->serial, context->major,
  1869. context->in_syscall, name->name,
  1870. context->name_count, context->put_count);
  1871. dump_stack();
  1872. }
  1873. }
  1874. #endif
  1875. }
  1876. static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
  1877. {
  1878. struct cpu_vfs_cap_data caps;
  1879. int rc;
  1880. if (!dentry)
  1881. return 0;
  1882. rc = get_vfs_caps_from_disk(dentry, &caps);
  1883. if (rc)
  1884. return rc;
  1885. name->fcap.permitted = caps.permitted;
  1886. name->fcap.inheritable = caps.inheritable;
  1887. name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  1888. name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  1889. return 0;
  1890. }
  1891. /* Copy inode data into an audit_names. */
  1892. static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
  1893. const struct inode *inode)
  1894. {
  1895. name->ino = inode->i_ino;
  1896. name->dev = inode->i_sb->s_dev;
  1897. name->mode = inode->i_mode;
  1898. name->uid = inode->i_uid;
  1899. name->gid = inode->i_gid;
  1900. name->rdev = inode->i_rdev;
  1901. security_inode_getsecid(inode, &name->osid);
  1902. audit_copy_fcaps(name, dentry);
  1903. }
  1904. /**
  1905. * __audit_inode - store the inode and device from a lookup
  1906. * @name: name being audited
  1907. * @dentry: dentry being audited
  1908. * @parent: does this dentry represent the parent?
  1909. */
  1910. void __audit_inode(struct filename *name, const struct dentry *dentry,
  1911. unsigned int parent)
  1912. {
  1913. struct audit_context *context = current->audit_context;
  1914. const struct inode *inode = dentry->d_inode;
  1915. struct audit_names *n;
  1916. if (!context->in_syscall)
  1917. return;
  1918. if (!name)
  1919. goto out_alloc;
  1920. #if AUDIT_DEBUG
  1921. /* The struct filename _must_ have a populated ->name */
  1922. BUG_ON(!name->name);
  1923. #endif
  1924. /*
  1925. * If we have a pointer to an audit_names entry already, then we can
  1926. * just use it directly if the type is correct.
  1927. */
  1928. n = name->aname;
  1929. if (n) {
  1930. if (parent) {
  1931. if (n->type == AUDIT_TYPE_PARENT ||
  1932. n->type == AUDIT_TYPE_UNKNOWN)
  1933. goto out;
  1934. } else {
  1935. if (n->type != AUDIT_TYPE_PARENT)
  1936. goto out;
  1937. }
  1938. }
  1939. list_for_each_entry_reverse(n, &context->names_list, list) {
  1940. /* does the name pointer match? */
  1941. if (!n->name || n->name->name != name->name)
  1942. continue;
  1943. /* match the correct record type */
  1944. if (parent) {
  1945. if (n->type == AUDIT_TYPE_PARENT ||
  1946. n->type == AUDIT_TYPE_UNKNOWN)
  1947. goto out;
  1948. } else {
  1949. if (n->type != AUDIT_TYPE_PARENT)
  1950. goto out;
  1951. }
  1952. }
  1953. out_alloc:
  1954. /* unable to find the name from a previous getname(). Allocate a new
  1955. * anonymous entry.
  1956. */
  1957. n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
  1958. if (!n)
  1959. return;
  1960. out:
  1961. if (parent) {
  1962. n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
  1963. n->type = AUDIT_TYPE_PARENT;
  1964. } else {
  1965. n->name_len = AUDIT_NAME_FULL;
  1966. n->type = AUDIT_TYPE_NORMAL;
  1967. }
  1968. handle_path(dentry);
  1969. audit_copy_inode(n, dentry, inode);
  1970. }
  1971. /**
  1972. * __audit_inode_child - collect inode info for created/removed objects
  1973. * @parent: inode of dentry parent
  1974. * @dentry: dentry being audited
  1975. * @type: AUDIT_TYPE_* value that we're looking for
  1976. *
  1977. * For syscalls that create or remove filesystem objects, audit_inode
  1978. * can only collect information for the filesystem object's parent.
  1979. * This call updates the audit context with the child's information.
  1980. * Syscalls that create a new filesystem object must be hooked after
  1981. * the object is created. Syscalls that remove a filesystem object
  1982. * must be hooked prior, in order to capture the target inode during
  1983. * unsuccessful attempts.
  1984. */
  1985. void __audit_inode_child(const struct inode *parent,
  1986. const struct dentry *dentry,
  1987. const unsigned char type)
  1988. {
  1989. struct audit_context *context = current->audit_context;
  1990. const struct inode *inode = dentry->d_inode;
  1991. const char *dname = dentry->d_name.name;
  1992. struct audit_names *n, *found_parent = NULL, *found_child = NULL;
  1993. if (!context->in_syscall)
  1994. return;
  1995. if (inode)
  1996. handle_one(inode);
  1997. /* look for a parent entry first */
  1998. list_for_each_entry(n, &context->names_list, list) {
  1999. if (!n->name || n->type != AUDIT_TYPE_PARENT)
  2000. continue;
  2001. if (n->ino == parent->i_ino &&
  2002. !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
  2003. found_parent = n;
  2004. break;
  2005. }
  2006. }
  2007. /* is there a matching child entry? */
  2008. list_for_each_entry(n, &context->names_list, list) {
  2009. /* can only match entries that have a name */
  2010. if (!n->name || n->type != type)
  2011. continue;
  2012. /* if we found a parent, make sure this one is a child of it */
  2013. if (found_parent && (n->name != found_parent->name))
  2014. continue;
  2015. if (!strcmp(dname, n->name->name) ||
  2016. !audit_compare_dname_path(dname, n->name->name,
  2017. found_parent ?
  2018. found_parent->name_len :
  2019. AUDIT_NAME_FULL)) {
  2020. found_child = n;
  2021. break;
  2022. }
  2023. }
  2024. if (!found_parent) {
  2025. /* create a new, "anonymous" parent record */
  2026. n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
  2027. if (!n)
  2028. return;
  2029. audit_copy_inode(n, NULL, parent);
  2030. }
  2031. if (!found_child) {
  2032. found_child = audit_alloc_name(context, type);
  2033. if (!found_child)
  2034. return;
  2035. /* Re-use the name belonging to the slot for a matching parent
  2036. * directory. All names for this context are relinquished in
  2037. * audit_free_names() */
  2038. if (found_parent) {
  2039. found_child->name = found_parent->name;
  2040. found_child->name_len = AUDIT_NAME_FULL;
  2041. /* don't call __putname() */
  2042. found_child->name_put = false;
  2043. }
  2044. }
  2045. if (inode)
  2046. audit_copy_inode(found_child, dentry, inode);
  2047. else
  2048. found_child->ino = (unsigned long)-1;
  2049. }
  2050. EXPORT_SYMBOL_GPL(__audit_inode_child);
  2051. /**
  2052. * auditsc_get_stamp - get local copies of audit_context values
  2053. * @ctx: audit_context for the task
  2054. * @t: timespec to store time recorded in the audit_context
  2055. * @serial: serial value that is recorded in the audit_context
  2056. *
  2057. * Also sets the context as auditable.
  2058. */
  2059. int auditsc_get_stamp(struct audit_context *ctx,
  2060. struct timespec *t, unsigned int *serial)
  2061. {
  2062. if (!ctx->in_syscall)
  2063. return 0;
  2064. if (!ctx->serial)
  2065. ctx->serial = audit_serial();
  2066. t->tv_sec = ctx->ctime.tv_sec;
  2067. t->tv_nsec = ctx->ctime.tv_nsec;
  2068. *serial = ctx->serial;
  2069. if (!ctx->prio) {
  2070. ctx->prio = 1;
  2071. ctx->current_state = AUDIT_RECORD_CONTEXT;
  2072. }
  2073. return 1;
  2074. }
  2075. /* global counter which is incremented every time something logs in */
  2076. static atomic_t session_id = ATOMIC_INIT(0);
  2077. /**
  2078. * audit_set_loginuid - set current task's audit_context loginuid
  2079. * @loginuid: loginuid value
  2080. *
  2081. * Returns 0.
  2082. *
  2083. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  2084. */
  2085. int audit_set_loginuid(kuid_t loginuid)
  2086. {
  2087. struct task_struct *task = current;
  2088. struct audit_context *context = task->audit_context;
  2089. unsigned int sessionid;
  2090. #ifdef CONFIG_AUDIT_LOGINUID_IMMUTABLE
  2091. if (uid_valid(task->loginuid))
  2092. return -EPERM;
  2093. #else /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
  2094. if (!capable(CAP_AUDIT_CONTROL))
  2095. return -EPERM;
  2096. #endif /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
  2097. sessionid = atomic_inc_return(&session_id);
  2098. if (context && context->in_syscall) {
  2099. struct audit_buffer *ab;
  2100. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  2101. if (ab) {
  2102. audit_log_format(ab, "login pid=%d uid=%u "
  2103. "old auid=%u new auid=%u"
  2104. " old ses=%u new ses=%u",
  2105. task->pid,
  2106. from_kuid(&init_user_ns, task_uid(task)),
  2107. from_kuid(&init_user_ns, task->loginuid),
  2108. from_kuid(&init_user_ns, loginuid),
  2109. task->sessionid, sessionid);
  2110. audit_log_end(ab);
  2111. }
  2112. }
  2113. task->sessionid = sessionid;
  2114. task->loginuid = loginuid;
  2115. return 0;
  2116. }
  2117. /**
  2118. * __audit_mq_open - record audit data for a POSIX MQ open
  2119. * @oflag: open flag
  2120. * @mode: mode bits
  2121. * @attr: queue attributes
  2122. *
  2123. */
  2124. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  2125. {
  2126. struct audit_context *context = current->audit_context;
  2127. if (attr)
  2128. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  2129. else
  2130. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  2131. context->mq_open.oflag = oflag;
  2132. context->mq_open.mode = mode;
  2133. context->type = AUDIT_MQ_OPEN;
  2134. }
  2135. /**
  2136. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  2137. * @mqdes: MQ descriptor
  2138. * @msg_len: Message length
  2139. * @msg_prio: Message priority
  2140. * @abs_timeout: Message timeout in absolute time
  2141. *
  2142. */
  2143. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  2144. const struct timespec *abs_timeout)
  2145. {
  2146. struct audit_context *context = current->audit_context;
  2147. struct timespec *p = &context->mq_sendrecv.abs_timeout;
  2148. if (abs_timeout)
  2149. memcpy(p, abs_timeout, sizeof(struct timespec));
  2150. else
  2151. memset(p, 0, sizeof(struct timespec));
  2152. context->mq_sendrecv.mqdes = mqdes;
  2153. context->mq_sendrecv.msg_len = msg_len;
  2154. context->mq_sendrecv.msg_prio = msg_prio;
  2155. context->type = AUDIT_MQ_SENDRECV;
  2156. }
  2157. /**
  2158. * __audit_mq_notify - record audit data for a POSIX MQ notify
  2159. * @mqdes: MQ descriptor
  2160. * @notification: Notification event
  2161. *
  2162. */
  2163. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  2164. {
  2165. struct audit_context *context = current->audit_context;
  2166. if (notification)
  2167. context->mq_notify.sigev_signo = notification->sigev_signo;
  2168. else
  2169. context->mq_notify.sigev_signo = 0;
  2170. context->mq_notify.mqdes = mqdes;
  2171. context->type = AUDIT_MQ_NOTIFY;
  2172. }
  2173. /**
  2174. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  2175. * @mqdes: MQ descriptor
  2176. * @mqstat: MQ flags
  2177. *
  2178. */
  2179. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  2180. {
  2181. struct audit_context *context = current->audit_context;
  2182. context->mq_getsetattr.mqdes = mqdes;
  2183. context->mq_getsetattr.mqstat = *mqstat;
  2184. context->type = AUDIT_MQ_GETSETATTR;
  2185. }
  2186. /**
  2187. * audit_ipc_obj - record audit data for ipc object
  2188. * @ipcp: ipc permissions
  2189. *
  2190. */
  2191. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  2192. {
  2193. struct audit_context *context = current->audit_context;
  2194. context->ipc.uid = ipcp->uid;
  2195. context->ipc.gid = ipcp->gid;
  2196. context->ipc.mode = ipcp->mode;
  2197. context->ipc.has_perm = 0;
  2198. security_ipc_getsecid(ipcp, &context->ipc.osid);
  2199. context->type = AUDIT_IPC;
  2200. }
  2201. /**
  2202. * audit_ipc_set_perm - record audit data for new ipc permissions
  2203. * @qbytes: msgq bytes
  2204. * @uid: msgq user id
  2205. * @gid: msgq group id
  2206. * @mode: msgq mode (permissions)
  2207. *
  2208. * Called only after audit_ipc_obj().
  2209. */
  2210. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  2211. {
  2212. struct audit_context *context = current->audit_context;
  2213. context->ipc.qbytes = qbytes;
  2214. context->ipc.perm_uid = uid;
  2215. context->ipc.perm_gid = gid;
  2216. context->ipc.perm_mode = mode;
  2217. context->ipc.has_perm = 1;
  2218. }
  2219. int __audit_bprm(struct linux_binprm *bprm)
  2220. {
  2221. struct audit_aux_data_execve *ax;
  2222. struct audit_context *context = current->audit_context;
  2223. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2224. if (!ax)
  2225. return -ENOMEM;
  2226. ax->argc = bprm->argc;
  2227. ax->envc = bprm->envc;
  2228. ax->mm = bprm->mm;
  2229. ax->d.type = AUDIT_EXECVE;
  2230. ax->d.next = context->aux;
  2231. context->aux = (void *)ax;
  2232. return 0;
  2233. }
  2234. /**
  2235. * audit_socketcall - record audit data for sys_socketcall
  2236. * @nargs: number of args
  2237. * @args: args array
  2238. *
  2239. */
  2240. void __audit_socketcall(int nargs, unsigned long *args)
  2241. {
  2242. struct audit_context *context = current->audit_context;
  2243. context->type = AUDIT_SOCKETCALL;
  2244. context->socketcall.nargs = nargs;
  2245. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  2246. }
  2247. /**
  2248. * __audit_fd_pair - record audit data for pipe and socketpair
  2249. * @fd1: the first file descriptor
  2250. * @fd2: the second file descriptor
  2251. *
  2252. */
  2253. void __audit_fd_pair(int fd1, int fd2)
  2254. {
  2255. struct audit_context *context = current->audit_context;
  2256. context->fds[0] = fd1;
  2257. context->fds[1] = fd2;
  2258. }
  2259. /**
  2260. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2261. * @len: data length in user space
  2262. * @a: data address in kernel space
  2263. *
  2264. * Returns 0 for success or NULL context or < 0 on error.
  2265. */
  2266. int __audit_sockaddr(int len, void *a)
  2267. {
  2268. struct audit_context *context = current->audit_context;
  2269. if (!context->sockaddr) {
  2270. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2271. if (!p)
  2272. return -ENOMEM;
  2273. context->sockaddr = p;
  2274. }
  2275. context->sockaddr_len = len;
  2276. memcpy(context->sockaddr, a, len);
  2277. return 0;
  2278. }
  2279. void __audit_ptrace(struct task_struct *t)
  2280. {
  2281. struct audit_context *context = current->audit_context;
  2282. context->target_pid = t->pid;
  2283. context->target_auid = audit_get_loginuid(t);
  2284. context->target_uid = task_uid(t);
  2285. context->target_sessionid = audit_get_sessionid(t);
  2286. security_task_getsecid(t, &context->target_sid);
  2287. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2288. }
  2289. /**
  2290. * audit_signal_info - record signal info for shutting down audit subsystem
  2291. * @sig: signal value
  2292. * @t: task being signaled
  2293. *
  2294. * If the audit subsystem is being terminated, record the task (pid)
  2295. * and uid that is doing that.
  2296. */
  2297. int __audit_signal_info(int sig, struct task_struct *t)
  2298. {
  2299. struct audit_aux_data_pids *axp;
  2300. struct task_struct *tsk = current;
  2301. struct audit_context *ctx = tsk->audit_context;
  2302. kuid_t uid = current_uid(), t_uid = task_uid(t);
  2303. if (audit_pid && t->tgid == audit_pid) {
  2304. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2305. audit_sig_pid = tsk->pid;
  2306. if (uid_valid(tsk->loginuid))
  2307. audit_sig_uid = tsk->loginuid;
  2308. else
  2309. audit_sig_uid = uid;
  2310. security_task_getsecid(tsk, &audit_sig_sid);
  2311. }
  2312. if (!audit_signals || audit_dummy_context())
  2313. return 0;
  2314. }
  2315. /* optimize the common case by putting first signal recipient directly
  2316. * in audit_context */
  2317. if (!ctx->target_pid) {
  2318. ctx->target_pid = t->tgid;
  2319. ctx->target_auid = audit_get_loginuid(t);
  2320. ctx->target_uid = t_uid;
  2321. ctx->target_sessionid = audit_get_sessionid(t);
  2322. security_task_getsecid(t, &ctx->target_sid);
  2323. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2324. return 0;
  2325. }
  2326. axp = (void *)ctx->aux_pids;
  2327. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2328. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2329. if (!axp)
  2330. return -ENOMEM;
  2331. axp->d.type = AUDIT_OBJ_PID;
  2332. axp->d.next = ctx->aux_pids;
  2333. ctx->aux_pids = (void *)axp;
  2334. }
  2335. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2336. axp->target_pid[axp->pid_count] = t->tgid;
  2337. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2338. axp->target_uid[axp->pid_count] = t_uid;
  2339. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2340. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2341. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2342. axp->pid_count++;
  2343. return 0;
  2344. }
  2345. /**
  2346. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2347. * @bprm: pointer to the bprm being processed
  2348. * @new: the proposed new credentials
  2349. * @old: the old credentials
  2350. *
  2351. * Simply check if the proc already has the caps given by the file and if not
  2352. * store the priv escalation info for later auditing at the end of the syscall
  2353. *
  2354. * -Eric
  2355. */
  2356. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2357. const struct cred *new, const struct cred *old)
  2358. {
  2359. struct audit_aux_data_bprm_fcaps *ax;
  2360. struct audit_context *context = current->audit_context;
  2361. struct cpu_vfs_cap_data vcaps;
  2362. struct dentry *dentry;
  2363. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2364. if (!ax)
  2365. return -ENOMEM;
  2366. ax->d.type = AUDIT_BPRM_FCAPS;
  2367. ax->d.next = context->aux;
  2368. context->aux = (void *)ax;
  2369. dentry = dget(bprm->file->f_dentry);
  2370. get_vfs_caps_from_disk(dentry, &vcaps);
  2371. dput(dentry);
  2372. ax->fcap.permitted = vcaps.permitted;
  2373. ax->fcap.inheritable = vcaps.inheritable;
  2374. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2375. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2376. ax->old_pcap.permitted = old->cap_permitted;
  2377. ax->old_pcap.inheritable = old->cap_inheritable;
  2378. ax->old_pcap.effective = old->cap_effective;
  2379. ax->new_pcap.permitted = new->cap_permitted;
  2380. ax->new_pcap.inheritable = new->cap_inheritable;
  2381. ax->new_pcap.effective = new->cap_effective;
  2382. return 0;
  2383. }
  2384. /**
  2385. * __audit_log_capset - store information about the arguments to the capset syscall
  2386. * @pid: target pid of the capset call
  2387. * @new: the new credentials
  2388. * @old: the old (current) credentials
  2389. *
  2390. * Record the aguments userspace sent to sys_capset for later printing by the
  2391. * audit system if applicable
  2392. */
  2393. void __audit_log_capset(pid_t pid,
  2394. const struct cred *new, const struct cred *old)
  2395. {
  2396. struct audit_context *context = current->audit_context;
  2397. context->capset.pid = pid;
  2398. context->capset.cap.effective = new->cap_effective;
  2399. context->capset.cap.inheritable = new->cap_effective;
  2400. context->capset.cap.permitted = new->cap_permitted;
  2401. context->type = AUDIT_CAPSET;
  2402. }
  2403. void __audit_mmap_fd(int fd, int flags)
  2404. {
  2405. struct audit_context *context = current->audit_context;
  2406. context->mmap.fd = fd;
  2407. context->mmap.flags = flags;
  2408. context->type = AUDIT_MMAP;
  2409. }
  2410. static void audit_log_task(struct audit_buffer *ab)
  2411. {
  2412. kuid_t auid, uid;
  2413. kgid_t gid;
  2414. unsigned int sessionid;
  2415. auid = audit_get_loginuid(current);
  2416. sessionid = audit_get_sessionid(current);
  2417. current_uid_gid(&uid, &gid);
  2418. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2419. from_kuid(&init_user_ns, auid),
  2420. from_kuid(&init_user_ns, uid),
  2421. from_kgid(&init_user_ns, gid),
  2422. sessionid);
  2423. audit_log_task_context(ab);
  2424. audit_log_format(ab, " pid=%d comm=", current->pid);
  2425. audit_log_untrustedstring(ab, current->comm);
  2426. }
  2427. static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
  2428. {
  2429. audit_log_task(ab);
  2430. audit_log_format(ab, " reason=");
  2431. audit_log_string(ab, reason);
  2432. audit_log_format(ab, " sig=%ld", signr);
  2433. }
  2434. /**
  2435. * audit_core_dumps - record information about processes that end abnormally
  2436. * @signr: signal value
  2437. *
  2438. * If a process ends with a core dump, something fishy is going on and we
  2439. * should record the event for investigation.
  2440. */
  2441. void audit_core_dumps(long signr)
  2442. {
  2443. struct audit_buffer *ab;
  2444. if (!audit_enabled)
  2445. return;
  2446. if (signr == SIGQUIT) /* don't care for those */
  2447. return;
  2448. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2449. if (unlikely(!ab))
  2450. return;
  2451. audit_log_abend(ab, "memory violation", signr);
  2452. audit_log_end(ab);
  2453. }
  2454. void __audit_seccomp(unsigned long syscall, long signr, int code)
  2455. {
  2456. struct audit_buffer *ab;
  2457. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
  2458. if (unlikely(!ab))
  2459. return;
  2460. audit_log_task(ab);
  2461. audit_log_format(ab, " sig=%ld", signr);
  2462. audit_log_format(ab, " syscall=%ld", syscall);
  2463. audit_log_format(ab, " compat=%d", is_compat_task());
  2464. audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
  2465. audit_log_format(ab, " code=0x%x", code);
  2466. audit_log_end(ab);
  2467. }
  2468. struct list_head *audit_killed_trees(void)
  2469. {
  2470. struct audit_context *ctx = current->audit_context;
  2471. if (likely(!ctx || !ctx->in_syscall))
  2472. return NULL;
  2473. return &ctx->killed_trees;
  2474. }