auditsc.c 72 KB

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