auditsc.c 71 KB

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