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