auditsc.c 68 KB

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