auditsc.c 64 KB

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