smackfs.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /*
  2. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2.
  7. *
  8. * Authors:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. * Ahmed S. Darwish <darwish.07@gmail.com>
  11. *
  12. * Special thanks to the authors of selinuxfs.
  13. *
  14. * Karl MacMillan <kmacmillan@tresys.com>
  15. * James Morris <jmorris@redhat.com>
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/security.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cipso_ipv4.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/ctype.h>
  27. #include <linux/audit.h>
  28. #include "smack.h"
  29. /*
  30. * smackfs pseudo filesystem.
  31. */
  32. enum smk_inos {
  33. SMK_ROOT_INO = 2,
  34. SMK_LOAD = 3, /* load policy */
  35. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  36. SMK_DOI = 5, /* CIPSO DOI */
  37. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  38. SMK_AMBIENT = 7, /* internet ambient label */
  39. SMK_NETLBLADDR = 8, /* single label hosts */
  40. SMK_ONLYCAP = 9, /* the only "capable" label */
  41. SMK_LOGGING = 10, /* logging */
  42. SMK_LOAD_SELF = 11, /* task specific rules */
  43. SMK_ACCESSES = 12, /* access policy */
  44. SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
  45. SMK_LOAD2 = 14, /* load policy with long labels */
  46. SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
  47. SMK_ACCESS2 = 16, /* make an access check with long labels */
  48. SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
  49. };
  50. /*
  51. * List locks
  52. */
  53. static DEFINE_MUTEX(smack_list_lock);
  54. static DEFINE_MUTEX(smack_cipso_lock);
  55. static DEFINE_MUTEX(smack_ambient_lock);
  56. static DEFINE_MUTEX(smk_netlbladdr_lock);
  57. /*
  58. * This is the "ambient" label for network traffic.
  59. * If it isn't somehow marked, use this.
  60. * It can be reset via smackfs/ambient
  61. */
  62. char *smack_net_ambient;
  63. /*
  64. * This is the level in a CIPSO header that indicates a
  65. * smack label is contained directly in the category set.
  66. * It can be reset via smackfs/direct
  67. */
  68. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  69. /*
  70. * This is the level in a CIPSO header that indicates a
  71. * secid is contained directly in the category set.
  72. * It can be reset via smackfs/mapped
  73. */
  74. int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
  75. /*
  76. * Unless a process is running with this label even
  77. * having CAP_MAC_OVERRIDE isn't enough to grant
  78. * privilege to violate MAC policy. If no label is
  79. * designated (the NULL case) capabilities apply to
  80. * everyone. It is expected that the hat (^) label
  81. * will be used if any label is used.
  82. */
  83. char *smack_onlycap;
  84. /*
  85. * Certain IP addresses may be designated as single label hosts.
  86. * Packets are sent there unlabeled, but only from tasks that
  87. * can write to the specified label.
  88. */
  89. LIST_HEAD(smk_netlbladdr_list);
  90. /*
  91. * Rule lists are maintained for each label.
  92. * This master list is just for reading /smack/load and /smack/load2.
  93. */
  94. struct smack_master_list {
  95. struct list_head list;
  96. struct smack_rule *smk_rule;
  97. };
  98. LIST_HEAD(smack_rule_list);
  99. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  100. const char *smack_cipso_option = SMACK_CIPSO_OPTION;
  101. /*
  102. * Values for parsing cipso rules
  103. * SMK_DIGITLEN: Length of a digit field in a rule.
  104. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  105. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  106. */
  107. #define SMK_DIGITLEN 4
  108. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  109. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  110. /*
  111. * Values for parsing MAC rules
  112. * SMK_ACCESS: Maximum possible combination of access permissions
  113. * SMK_ACCESSLEN: Maximum length for a rule access field
  114. * SMK_LOADLEN: Smack rule length
  115. */
  116. #define SMK_OACCESS "rwxa"
  117. #define SMK_ACCESS "rwxat"
  118. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  119. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  120. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  121. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  122. /*
  123. * Stricly for CIPSO level manipulation.
  124. * Set the category bit number in a smack label sized buffer.
  125. */
  126. static inline void smack_catset_bit(unsigned int cat, char *catsetp)
  127. {
  128. if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
  129. return;
  130. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  131. }
  132. /**
  133. * smk_netlabel_audit_set - fill a netlbl_audit struct
  134. * @nap: structure to fill
  135. */
  136. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  137. {
  138. nap->loginuid = audit_get_loginuid(current);
  139. nap->sessionid = audit_get_sessionid(current);
  140. nap->secid = smack_to_secid(smk_of_current());
  141. }
  142. /*
  143. * Value for parsing single label host rules
  144. * "1.2.3.4 X"
  145. */
  146. #define SMK_NETLBLADDRMIN 9
  147. /**
  148. * smk_set_access - add a rule to the rule list
  149. * @srp: the new rule to add
  150. * @rule_list: the list of rules
  151. * @rule_lock: the rule list lock
  152. *
  153. * Looks through the current subject/object/access list for
  154. * the subject/object pair and replaces the access that was
  155. * there. If the pair isn't found add it with the specified
  156. * access.
  157. *
  158. * Returns 1 if a rule was found to exist already, 0 if it is new
  159. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  160. * during the allocation of the new pair to add.
  161. */
  162. static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list,
  163. struct mutex *rule_lock)
  164. {
  165. struct smack_rule *sp;
  166. int found = 0;
  167. mutex_lock(rule_lock);
  168. /*
  169. * Because the object label is less likely to match
  170. * than the subject label check it first
  171. */
  172. list_for_each_entry_rcu(sp, rule_list, list) {
  173. if (sp->smk_object == srp->smk_object &&
  174. sp->smk_subject == srp->smk_subject) {
  175. found = 1;
  176. sp->smk_access = srp->smk_access;
  177. break;
  178. }
  179. }
  180. if (found == 0)
  181. list_add_rcu(&srp->list, rule_list);
  182. mutex_unlock(rule_lock);
  183. return found;
  184. }
  185. /**
  186. * smk_fill_rule - Fill Smack rule from strings
  187. * @subject: subject label string
  188. * @object: object label string
  189. * @access: access string
  190. * @rule: Smack rule
  191. * @import: if non-zero, import labels
  192. *
  193. * Returns 0 on success, -1 on failure
  194. */
  195. static int smk_fill_rule(const char *subject, const char *object,
  196. const char *access, struct smack_rule *rule,
  197. int import)
  198. {
  199. int rc = -1;
  200. int done;
  201. const char *cp;
  202. struct smack_known *skp;
  203. if (import) {
  204. rule->smk_subject = smk_import(subject, 0);
  205. if (rule->smk_subject == NULL)
  206. return -1;
  207. rule->smk_object = smk_import(object, 0);
  208. if (rule->smk_object == NULL)
  209. return -1;
  210. } else {
  211. cp = smk_parse_smack(subject, 0);
  212. if (cp == NULL)
  213. return -1;
  214. skp = smk_find_entry(cp);
  215. kfree(cp);
  216. if (skp == NULL)
  217. return -1;
  218. rule->smk_subject = skp->smk_known;
  219. cp = smk_parse_smack(object, 0);
  220. if (cp == NULL)
  221. return -1;
  222. skp = smk_find_entry(cp);
  223. kfree(cp);
  224. if (skp == NULL)
  225. return -1;
  226. rule->smk_object = skp->smk_known;
  227. }
  228. rule->smk_access = 0;
  229. for (cp = access, done = 0; *cp && !done; cp++) {
  230. switch (*cp) {
  231. case '-':
  232. break;
  233. case 'r':
  234. case 'R':
  235. rule->smk_access |= MAY_READ;
  236. break;
  237. case 'w':
  238. case 'W':
  239. rule->smk_access |= MAY_WRITE;
  240. break;
  241. case 'x':
  242. case 'X':
  243. rule->smk_access |= MAY_EXEC;
  244. break;
  245. case 'a':
  246. case 'A':
  247. rule->smk_access |= MAY_APPEND;
  248. break;
  249. case 't':
  250. case 'T':
  251. rule->smk_access |= MAY_TRANSMUTE;
  252. break;
  253. default:
  254. done = 1;
  255. break;
  256. }
  257. }
  258. rc = 0;
  259. return rc;
  260. }
  261. /**
  262. * smk_parse_rule - parse Smack rule from load string
  263. * @data: string to be parsed whose size is SMK_LOADLEN
  264. * @rule: Smack rule
  265. * @import: if non-zero, import labels
  266. *
  267. * Returns 0 on success, -1 on errors.
  268. */
  269. static int smk_parse_rule(const char *data, struct smack_rule *rule, int import)
  270. {
  271. int rc;
  272. rc = smk_fill_rule(data, data + SMK_LABELLEN,
  273. data + SMK_LABELLEN + SMK_LABELLEN, rule, import);
  274. return rc;
  275. }
  276. /**
  277. * smk_parse_long_rule - parse Smack rule from rule string
  278. * @data: string to be parsed, null terminated
  279. * @rule: Smack rule
  280. * @import: if non-zero, import labels
  281. *
  282. * Returns 0 on success, -1 on failure
  283. */
  284. static int smk_parse_long_rule(const char *data, struct smack_rule *rule,
  285. int import)
  286. {
  287. char *subject;
  288. char *object;
  289. char *access;
  290. int datalen;
  291. int rc = -1;
  292. /*
  293. * This is probably inefficient, but safe.
  294. */
  295. datalen = strlen(data);
  296. subject = kzalloc(datalen, GFP_KERNEL);
  297. if (subject == NULL)
  298. return -1;
  299. object = kzalloc(datalen, GFP_KERNEL);
  300. if (object == NULL)
  301. goto free_out_s;
  302. access = kzalloc(datalen, GFP_KERNEL);
  303. if (access == NULL)
  304. goto free_out_o;
  305. if (sscanf(data, "%s %s %s", subject, object, access) == 3)
  306. rc = smk_fill_rule(subject, object, access, rule, import);
  307. kfree(access);
  308. free_out_o:
  309. kfree(object);
  310. free_out_s:
  311. kfree(subject);
  312. return rc;
  313. }
  314. #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
  315. #define SMK_LONG_FMT 1 /* Variable long label format */
  316. /**
  317. * smk_write_rules_list - write() for any /smack rule file
  318. * @file: file pointer, not actually used
  319. * @buf: where to get the data from
  320. * @count: bytes sent
  321. * @ppos: where to start - must be 0
  322. * @rule_list: the list of rules to write to
  323. * @rule_lock: lock for the rule list
  324. * @format: /smack/load or /smack/load2 format.
  325. *
  326. * Get one smack access rule from above.
  327. * The format for SMK_LONG_FMT is:
  328. * "subject<whitespace>object<whitespace>access[<whitespace>...]"
  329. * The format for SMK_FIXED24_FMT is exactly:
  330. * "subject object rwxat"
  331. */
  332. static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
  333. size_t count, loff_t *ppos,
  334. struct list_head *rule_list,
  335. struct mutex *rule_lock, int format)
  336. {
  337. struct smack_master_list *smlp;
  338. struct smack_known *skp;
  339. struct smack_rule *rule;
  340. char *data;
  341. int datalen;
  342. int rc = -EINVAL;
  343. int load = 0;
  344. /*
  345. * No partial writes.
  346. * Enough data must be present.
  347. */
  348. if (*ppos != 0)
  349. return -EINVAL;
  350. if (format == SMK_FIXED24_FMT) {
  351. /*
  352. * Minor hack for backward compatibility
  353. */
  354. if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
  355. return -EINVAL;
  356. datalen = SMK_LOADLEN;
  357. } else
  358. datalen = count + 1;
  359. data = kzalloc(datalen, GFP_KERNEL);
  360. if (data == NULL)
  361. return -ENOMEM;
  362. if (copy_from_user(data, buf, count) != 0) {
  363. rc = -EFAULT;
  364. goto out;
  365. }
  366. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  367. if (rule == NULL) {
  368. rc = -ENOMEM;
  369. goto out;
  370. }
  371. if (format == SMK_LONG_FMT) {
  372. /*
  373. * Be sure the data string is terminated.
  374. */
  375. data[count] = '\0';
  376. if (smk_parse_long_rule(data, rule, 1))
  377. goto out_free_rule;
  378. } else {
  379. /*
  380. * More on the minor hack for backward compatibility
  381. */
  382. if (count == (SMK_OLOADLEN))
  383. data[SMK_OLOADLEN] = '-';
  384. if (smk_parse_rule(data, rule, 1))
  385. goto out_free_rule;
  386. }
  387. if (rule_list == NULL) {
  388. load = 1;
  389. skp = smk_find_entry(rule->smk_subject);
  390. rule_list = &skp->smk_rules;
  391. rule_lock = &skp->smk_rules_lock;
  392. }
  393. rc = count;
  394. /*
  395. * If this is a global as opposed to self and a new rule
  396. * it needs to get added for reporting.
  397. * smk_set_access returns true if there was already a rule
  398. * for the subject/object pair, and false if it was new.
  399. */
  400. if (!smk_set_access(rule, rule_list, rule_lock)) {
  401. if (load) {
  402. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  403. if (smlp != NULL) {
  404. smlp->smk_rule = rule;
  405. list_add_rcu(&smlp->list, &smack_rule_list);
  406. } else
  407. rc = -ENOMEM;
  408. }
  409. goto out;
  410. }
  411. out_free_rule:
  412. kfree(rule);
  413. out:
  414. kfree(data);
  415. return rc;
  416. }
  417. /*
  418. * Core logic for smackfs seq list operations.
  419. */
  420. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  421. struct list_head *head)
  422. {
  423. struct list_head *list;
  424. /*
  425. * This is 0 the first time through.
  426. */
  427. if (s->index == 0)
  428. s->private = head;
  429. if (s->private == NULL)
  430. return NULL;
  431. list = s->private;
  432. if (list_empty(list))
  433. return NULL;
  434. if (s->index == 0)
  435. return list->next;
  436. return list;
  437. }
  438. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  439. struct list_head *head)
  440. {
  441. struct list_head *list = v;
  442. if (list_is_last(list, head)) {
  443. s->private = NULL;
  444. return NULL;
  445. }
  446. s->private = list->next;
  447. return list->next;
  448. }
  449. static void smk_seq_stop(struct seq_file *s, void *v)
  450. {
  451. /* No-op */
  452. }
  453. static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
  454. {
  455. /*
  456. * Don't show any rules with label names too long for
  457. * interface file (/smack/load or /smack/load2)
  458. * because you should expect to be able to write
  459. * anything you read back.
  460. */
  461. if (strlen(srp->smk_subject) >= max || strlen(srp->smk_object) >= max)
  462. return;
  463. seq_printf(s, "%s %s", srp->smk_subject, srp->smk_object);
  464. seq_putc(s, ' ');
  465. if (srp->smk_access & MAY_READ)
  466. seq_putc(s, 'r');
  467. if (srp->smk_access & MAY_WRITE)
  468. seq_putc(s, 'w');
  469. if (srp->smk_access & MAY_EXEC)
  470. seq_putc(s, 'x');
  471. if (srp->smk_access & MAY_APPEND)
  472. seq_putc(s, 'a');
  473. if (srp->smk_access & MAY_TRANSMUTE)
  474. seq_putc(s, 't');
  475. if (srp->smk_access == 0)
  476. seq_putc(s, '-');
  477. seq_putc(s, '\n');
  478. }
  479. /*
  480. * Seq_file read operations for /smack/load
  481. */
  482. static void *load2_seq_start(struct seq_file *s, loff_t *pos)
  483. {
  484. return smk_seq_start(s, pos, &smack_rule_list);
  485. }
  486. static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  487. {
  488. return smk_seq_next(s, v, pos, &smack_rule_list);
  489. }
  490. static int load_seq_show(struct seq_file *s, void *v)
  491. {
  492. struct list_head *list = v;
  493. struct smack_master_list *smlp =
  494. list_entry(list, struct smack_master_list, list);
  495. smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
  496. return 0;
  497. }
  498. static const struct seq_operations load_seq_ops = {
  499. .start = load2_seq_start,
  500. .next = load2_seq_next,
  501. .show = load_seq_show,
  502. .stop = smk_seq_stop,
  503. };
  504. /**
  505. * smk_open_load - open() for /smack/load
  506. * @inode: inode structure representing file
  507. * @file: "load" file pointer
  508. *
  509. * For reading, use load_seq_* seq_file reading operations.
  510. */
  511. static int smk_open_load(struct inode *inode, struct file *file)
  512. {
  513. return seq_open(file, &load_seq_ops);
  514. }
  515. /**
  516. * smk_write_load - write() for /smack/load
  517. * @file: file pointer, not actually used
  518. * @buf: where to get the data from
  519. * @count: bytes sent
  520. * @ppos: where to start - must be 0
  521. *
  522. */
  523. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  524. size_t count, loff_t *ppos)
  525. {
  526. /*
  527. * Must have privilege.
  528. * No partial writes.
  529. * Enough data must be present.
  530. */
  531. if (!capable(CAP_MAC_ADMIN))
  532. return -EPERM;
  533. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  534. SMK_FIXED24_FMT);
  535. }
  536. static const struct file_operations smk_load_ops = {
  537. .open = smk_open_load,
  538. .read = seq_read,
  539. .llseek = seq_lseek,
  540. .write = smk_write_load,
  541. .release = seq_release,
  542. };
  543. /**
  544. * smk_cipso_doi - initialize the CIPSO domain
  545. */
  546. static void smk_cipso_doi(void)
  547. {
  548. int rc;
  549. struct cipso_v4_doi *doip;
  550. struct netlbl_audit nai;
  551. smk_netlabel_audit_set(&nai);
  552. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  553. if (rc != 0)
  554. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  555. __func__, __LINE__, rc);
  556. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  557. if (doip == NULL)
  558. panic("smack: Failed to initialize cipso DOI.\n");
  559. doip->map.std = NULL;
  560. doip->doi = smk_cipso_doi_value;
  561. doip->type = CIPSO_V4_MAP_PASS;
  562. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  563. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  564. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  565. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  566. if (rc != 0) {
  567. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  568. __func__, __LINE__, rc);
  569. kfree(doip);
  570. return;
  571. }
  572. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  573. if (rc != 0) {
  574. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  575. __func__, __LINE__, rc);
  576. kfree(doip);
  577. return;
  578. }
  579. }
  580. /**
  581. * smk_unlbl_ambient - initialize the unlabeled domain
  582. * @oldambient: previous domain string
  583. */
  584. static void smk_unlbl_ambient(char *oldambient)
  585. {
  586. int rc;
  587. struct netlbl_audit nai;
  588. smk_netlabel_audit_set(&nai);
  589. if (oldambient != NULL) {
  590. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  591. if (rc != 0)
  592. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  593. __func__, __LINE__, rc);
  594. }
  595. if (smack_net_ambient == NULL)
  596. smack_net_ambient = smack_known_floor.smk_known;
  597. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
  598. NULL, NULL, &nai);
  599. if (rc != 0)
  600. printk(KERN_WARNING "%s:%d add rc = %d\n",
  601. __func__, __LINE__, rc);
  602. }
  603. /*
  604. * Seq_file read operations for /smack/cipso
  605. */
  606. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  607. {
  608. return smk_seq_start(s, pos, &smack_known_list);
  609. }
  610. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  611. {
  612. return smk_seq_next(s, v, pos, &smack_known_list);
  613. }
  614. /*
  615. * Print cipso labels in format:
  616. * label level[/cat[,cat]]
  617. */
  618. static int cipso_seq_show(struct seq_file *s, void *v)
  619. {
  620. struct list_head *list = v;
  621. struct smack_known *skp =
  622. list_entry(list, struct smack_known, list);
  623. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  624. char sep = '/';
  625. int i;
  626. /*
  627. * Don't show a label that could not have been set using
  628. * /smack/cipso. This is in support of the notion that
  629. * anything read from /smack/cipso ought to be writeable
  630. * to /smack/cipso.
  631. *
  632. * /smack/cipso2 should be used instead.
  633. */
  634. if (strlen(skp->smk_known) >= SMK_LABELLEN)
  635. return 0;
  636. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  637. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  638. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  639. seq_printf(s, "%c%d", sep, i);
  640. sep = ',';
  641. }
  642. seq_putc(s, '\n');
  643. return 0;
  644. }
  645. static const struct seq_operations cipso_seq_ops = {
  646. .start = cipso_seq_start,
  647. .next = cipso_seq_next,
  648. .show = cipso_seq_show,
  649. .stop = smk_seq_stop,
  650. };
  651. /**
  652. * smk_open_cipso - open() for /smack/cipso
  653. * @inode: inode structure representing file
  654. * @file: "cipso" file pointer
  655. *
  656. * Connect our cipso_seq_* operations with /smack/cipso
  657. * file_operations
  658. */
  659. static int smk_open_cipso(struct inode *inode, struct file *file)
  660. {
  661. return seq_open(file, &cipso_seq_ops);
  662. }
  663. /**
  664. * smk_set_cipso - do the work for write() for cipso and cipso2
  665. * @file: file pointer, not actually used
  666. * @buf: where to get the data from
  667. * @count: bytes sent
  668. * @ppos: where to start
  669. * @format: /smack/cipso or /smack/cipso2
  670. *
  671. * Accepts only one cipso rule per write call.
  672. * Returns number of bytes written or error code, as appropriate
  673. */
  674. static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
  675. size_t count, loff_t *ppos, int format)
  676. {
  677. struct smack_known *skp;
  678. struct netlbl_lsm_secattr ncats;
  679. char mapcatset[SMK_CIPSOLEN];
  680. int maplevel;
  681. unsigned int cat;
  682. int catlen;
  683. ssize_t rc = -EINVAL;
  684. char *data = NULL;
  685. char *rule;
  686. int ret;
  687. int i;
  688. /*
  689. * Must have privilege.
  690. * No partial writes.
  691. * Enough data must be present.
  692. */
  693. if (!capable(CAP_MAC_ADMIN))
  694. return -EPERM;
  695. if (*ppos != 0)
  696. return -EINVAL;
  697. if (format == SMK_FIXED24_FMT &&
  698. (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
  699. return -EINVAL;
  700. data = kzalloc(count + 1, GFP_KERNEL);
  701. if (data == NULL)
  702. return -ENOMEM;
  703. if (copy_from_user(data, buf, count) != 0) {
  704. rc = -EFAULT;
  705. goto unlockedout;
  706. }
  707. data[count] = '\0';
  708. rule = data;
  709. /*
  710. * Only allow one writer at a time. Writes should be
  711. * quite rare and small in any case.
  712. */
  713. mutex_lock(&smack_cipso_lock);
  714. skp = smk_import_entry(rule, 0);
  715. if (skp == NULL)
  716. goto out;
  717. if (format == SMK_FIXED24_FMT)
  718. rule += SMK_LABELLEN;
  719. else
  720. rule += strlen(skp->smk_known);
  721. ret = sscanf(rule, "%d", &maplevel);
  722. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  723. goto out;
  724. rule += SMK_DIGITLEN;
  725. ret = sscanf(rule, "%d", &catlen);
  726. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  727. goto out;
  728. if (format == SMK_FIXED24_FMT &&
  729. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  730. goto out;
  731. memset(mapcatset, 0, sizeof(mapcatset));
  732. for (i = 0; i < catlen; i++) {
  733. rule += SMK_DIGITLEN;
  734. ret = sscanf(rule, "%u", &cat);
  735. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  736. goto out;
  737. smack_catset_bit(cat, mapcatset);
  738. }
  739. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  740. if (rc >= 0) {
  741. netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
  742. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  743. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  744. rc = count;
  745. }
  746. out:
  747. mutex_unlock(&smack_cipso_lock);
  748. unlockedout:
  749. kfree(data);
  750. return rc;
  751. }
  752. /**
  753. * smk_write_cipso - write() for /smack/cipso
  754. * @file: file pointer, not actually used
  755. * @buf: where to get the data from
  756. * @count: bytes sent
  757. * @ppos: where to start
  758. *
  759. * Accepts only one cipso rule per write call.
  760. * Returns number of bytes written or error code, as appropriate
  761. */
  762. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  763. size_t count, loff_t *ppos)
  764. {
  765. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  766. }
  767. static const struct file_operations smk_cipso_ops = {
  768. .open = smk_open_cipso,
  769. .read = seq_read,
  770. .llseek = seq_lseek,
  771. .write = smk_write_cipso,
  772. .release = seq_release,
  773. };
  774. /*
  775. * Seq_file read operations for /smack/cipso2
  776. */
  777. /*
  778. * Print cipso labels in format:
  779. * label level[/cat[,cat]]
  780. */
  781. static int cipso2_seq_show(struct seq_file *s, void *v)
  782. {
  783. struct list_head *list = v;
  784. struct smack_known *skp =
  785. list_entry(list, struct smack_known, list);
  786. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  787. char sep = '/';
  788. int i;
  789. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  790. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  791. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  792. seq_printf(s, "%c%d", sep, i);
  793. sep = ',';
  794. }
  795. seq_putc(s, '\n');
  796. return 0;
  797. }
  798. static const struct seq_operations cipso2_seq_ops = {
  799. .start = cipso_seq_start,
  800. .next = cipso_seq_next,
  801. .show = cipso2_seq_show,
  802. .stop = smk_seq_stop,
  803. };
  804. /**
  805. * smk_open_cipso2 - open() for /smack/cipso2
  806. * @inode: inode structure representing file
  807. * @file: "cipso2" file pointer
  808. *
  809. * Connect our cipso_seq_* operations with /smack/cipso2
  810. * file_operations
  811. */
  812. static int smk_open_cipso2(struct inode *inode, struct file *file)
  813. {
  814. return seq_open(file, &cipso2_seq_ops);
  815. }
  816. /**
  817. * smk_write_cipso2 - write() for /smack/cipso2
  818. * @file: file pointer, not actually used
  819. * @buf: where to get the data from
  820. * @count: bytes sent
  821. * @ppos: where to start
  822. *
  823. * Accepts only one cipso rule per write call.
  824. * Returns number of bytes written or error code, as appropriate
  825. */
  826. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  827. size_t count, loff_t *ppos)
  828. {
  829. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  830. }
  831. static const struct file_operations smk_cipso2_ops = {
  832. .open = smk_open_cipso2,
  833. .read = seq_read,
  834. .llseek = seq_lseek,
  835. .write = smk_write_cipso2,
  836. .release = seq_release,
  837. };
  838. /*
  839. * Seq_file read operations for /smack/netlabel
  840. */
  841. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  842. {
  843. return smk_seq_start(s, pos, &smk_netlbladdr_list);
  844. }
  845. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  846. {
  847. return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
  848. }
  849. #define BEBITS (sizeof(__be32) * 8)
  850. /*
  851. * Print host/label pairs
  852. */
  853. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  854. {
  855. struct list_head *list = v;
  856. struct smk_netlbladdr *skp =
  857. list_entry(list, struct smk_netlbladdr, list);
  858. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  859. int maskn;
  860. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  861. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  862. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  863. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  864. return 0;
  865. }
  866. static const struct seq_operations netlbladdr_seq_ops = {
  867. .start = netlbladdr_seq_start,
  868. .next = netlbladdr_seq_next,
  869. .show = netlbladdr_seq_show,
  870. .stop = smk_seq_stop,
  871. };
  872. /**
  873. * smk_open_netlbladdr - open() for /smack/netlabel
  874. * @inode: inode structure representing file
  875. * @file: "netlabel" file pointer
  876. *
  877. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  878. * file_operations
  879. */
  880. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  881. {
  882. return seq_open(file, &netlbladdr_seq_ops);
  883. }
  884. /**
  885. * smk_netlbladdr_insert
  886. * @new : netlabel to insert
  887. *
  888. * This helper insert netlabel in the smack_netlbladdrs list
  889. * sorted by netmask length (longest to smallest)
  890. * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
  891. *
  892. */
  893. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  894. {
  895. struct smk_netlbladdr *m, *m_next;
  896. if (list_empty(&smk_netlbladdr_list)) {
  897. list_add_rcu(&new->list, &smk_netlbladdr_list);
  898. return;
  899. }
  900. m = list_entry_rcu(smk_netlbladdr_list.next,
  901. struct smk_netlbladdr, list);
  902. /* the comparison '>' is a bit hacky, but works */
  903. if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
  904. list_add_rcu(&new->list, &smk_netlbladdr_list);
  905. return;
  906. }
  907. list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
  908. if (list_is_last(&m->list, &smk_netlbladdr_list)) {
  909. list_add_rcu(&new->list, &m->list);
  910. return;
  911. }
  912. m_next = list_entry_rcu(m->list.next,
  913. struct smk_netlbladdr, list);
  914. if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
  915. list_add_rcu(&new->list, &m->list);
  916. return;
  917. }
  918. }
  919. }
  920. /**
  921. * smk_write_netlbladdr - write() for /smack/netlabel
  922. * @file: file pointer, not actually used
  923. * @buf: where to get the data from
  924. * @count: bytes sent
  925. * @ppos: where to start
  926. *
  927. * Accepts only one netlbladdr per write call.
  928. * Returns number of bytes written or error code, as appropriate
  929. */
  930. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  931. size_t count, loff_t *ppos)
  932. {
  933. struct smk_netlbladdr *skp;
  934. struct sockaddr_in newname;
  935. char *smack;
  936. char *sp;
  937. char *data;
  938. char *host = (char *)&newname.sin_addr.s_addr;
  939. int rc;
  940. struct netlbl_audit audit_info;
  941. struct in_addr mask;
  942. unsigned int m;
  943. int found;
  944. u32 mask_bits = (1<<31);
  945. __be32 nsa;
  946. u32 temp_mask;
  947. /*
  948. * Must have privilege.
  949. * No partial writes.
  950. * Enough data must be present.
  951. * "<addr/mask, as a.b.c.d/e><space><label>"
  952. * "<addr, as a.b.c.d><space><label>"
  953. */
  954. if (!capable(CAP_MAC_ADMIN))
  955. return -EPERM;
  956. if (*ppos != 0)
  957. return -EINVAL;
  958. if (count < SMK_NETLBLADDRMIN)
  959. return -EINVAL;
  960. data = kzalloc(count + 1, GFP_KERNEL);
  961. if (data == NULL)
  962. return -ENOMEM;
  963. if (copy_from_user(data, buf, count) != 0) {
  964. rc = -EFAULT;
  965. goto free_data_out;
  966. }
  967. smack = kzalloc(count + 1, GFP_KERNEL);
  968. if (smack == NULL) {
  969. rc = -ENOMEM;
  970. goto free_data_out;
  971. }
  972. data[count] = '\0';
  973. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  974. &host[0], &host[1], &host[2], &host[3], &m, smack);
  975. if (rc != 6) {
  976. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  977. &host[0], &host[1], &host[2], &host[3], smack);
  978. if (rc != 5) {
  979. rc = -EINVAL;
  980. goto free_out;
  981. }
  982. m = BEBITS;
  983. }
  984. if (m > BEBITS) {
  985. rc = -EINVAL;
  986. goto free_out;
  987. }
  988. /*
  989. * If smack begins with '-', it is an option, don't import it
  990. */
  991. if (smack[0] != '-') {
  992. sp = smk_import(smack, 0);
  993. if (sp == NULL) {
  994. rc = -EINVAL;
  995. goto free_out;
  996. }
  997. } else {
  998. /* check known options */
  999. if (strcmp(smack, smack_cipso_option) == 0)
  1000. sp = (char *)smack_cipso_option;
  1001. else {
  1002. rc = -EINVAL;
  1003. goto free_out;
  1004. }
  1005. }
  1006. for (temp_mask = 0; m > 0; m--) {
  1007. temp_mask |= mask_bits;
  1008. mask_bits >>= 1;
  1009. }
  1010. mask.s_addr = cpu_to_be32(temp_mask);
  1011. newname.sin_addr.s_addr &= mask.s_addr;
  1012. /*
  1013. * Only allow one writer at a time. Writes should be
  1014. * quite rare and small in any case.
  1015. */
  1016. mutex_lock(&smk_netlbladdr_lock);
  1017. nsa = newname.sin_addr.s_addr;
  1018. /* try to find if the prefix is already in the list */
  1019. found = 0;
  1020. list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
  1021. if (skp->smk_host.sin_addr.s_addr == nsa &&
  1022. skp->smk_mask.s_addr == mask.s_addr) {
  1023. found = 1;
  1024. break;
  1025. }
  1026. }
  1027. smk_netlabel_audit_set(&audit_info);
  1028. if (found == 0) {
  1029. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  1030. if (skp == NULL)
  1031. rc = -ENOMEM;
  1032. else {
  1033. rc = 0;
  1034. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  1035. skp->smk_mask.s_addr = mask.s_addr;
  1036. skp->smk_label = sp;
  1037. smk_netlbladdr_insert(skp);
  1038. }
  1039. } else {
  1040. /* we delete the unlabeled entry, only if the previous label
  1041. * wasn't the special CIPSO option */
  1042. if (skp->smk_label != smack_cipso_option)
  1043. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1044. &skp->smk_host.sin_addr, &skp->smk_mask,
  1045. PF_INET, &audit_info);
  1046. else
  1047. rc = 0;
  1048. skp->smk_label = sp;
  1049. }
  1050. /*
  1051. * Now tell netlabel about the single label nature of
  1052. * this host so that incoming packets get labeled.
  1053. * but only if we didn't get the special CIPSO option
  1054. */
  1055. if (rc == 0 && sp != smack_cipso_option)
  1056. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1057. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  1058. smack_to_secid(skp->smk_label), &audit_info);
  1059. if (rc == 0)
  1060. rc = count;
  1061. mutex_unlock(&smk_netlbladdr_lock);
  1062. free_out:
  1063. kfree(smack);
  1064. free_data_out:
  1065. kfree(data);
  1066. return rc;
  1067. }
  1068. static const struct file_operations smk_netlbladdr_ops = {
  1069. .open = smk_open_netlbladdr,
  1070. .read = seq_read,
  1071. .llseek = seq_lseek,
  1072. .write = smk_write_netlbladdr,
  1073. .release = seq_release,
  1074. };
  1075. /**
  1076. * smk_read_doi - read() for /smack/doi
  1077. * @filp: file pointer, not actually used
  1078. * @buf: where to put the result
  1079. * @count: maximum to send along
  1080. * @ppos: where to start
  1081. *
  1082. * Returns number of bytes read or error code, as appropriate
  1083. */
  1084. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1085. size_t count, loff_t *ppos)
  1086. {
  1087. char temp[80];
  1088. ssize_t rc;
  1089. if (*ppos != 0)
  1090. return 0;
  1091. sprintf(temp, "%d", smk_cipso_doi_value);
  1092. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1093. return rc;
  1094. }
  1095. /**
  1096. * smk_write_doi - write() for /smack/doi
  1097. * @file: file pointer, not actually used
  1098. * @buf: where to get the data from
  1099. * @count: bytes sent
  1100. * @ppos: where to start
  1101. *
  1102. * Returns number of bytes written or error code, as appropriate
  1103. */
  1104. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  1105. size_t count, loff_t *ppos)
  1106. {
  1107. char temp[80];
  1108. int i;
  1109. if (!capable(CAP_MAC_ADMIN))
  1110. return -EPERM;
  1111. if (count >= sizeof(temp) || count == 0)
  1112. return -EINVAL;
  1113. if (copy_from_user(temp, buf, count) != 0)
  1114. return -EFAULT;
  1115. temp[count] = '\0';
  1116. if (sscanf(temp, "%d", &i) != 1)
  1117. return -EINVAL;
  1118. smk_cipso_doi_value = i;
  1119. smk_cipso_doi();
  1120. return count;
  1121. }
  1122. static const struct file_operations smk_doi_ops = {
  1123. .read = smk_read_doi,
  1124. .write = smk_write_doi,
  1125. .llseek = default_llseek,
  1126. };
  1127. /**
  1128. * smk_read_direct - read() for /smack/direct
  1129. * @filp: file pointer, not actually used
  1130. * @buf: where to put the result
  1131. * @count: maximum to send along
  1132. * @ppos: where to start
  1133. *
  1134. * Returns number of bytes read or error code, as appropriate
  1135. */
  1136. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1137. size_t count, loff_t *ppos)
  1138. {
  1139. char temp[80];
  1140. ssize_t rc;
  1141. if (*ppos != 0)
  1142. return 0;
  1143. sprintf(temp, "%d", smack_cipso_direct);
  1144. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1145. return rc;
  1146. }
  1147. /**
  1148. * smk_write_direct - write() for /smack/direct
  1149. * @file: file pointer, not actually used
  1150. * @buf: where to get the data from
  1151. * @count: bytes sent
  1152. * @ppos: where to start
  1153. *
  1154. * Returns number of bytes written or error code, as appropriate
  1155. */
  1156. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1157. size_t count, loff_t *ppos)
  1158. {
  1159. struct smack_known *skp;
  1160. char temp[80];
  1161. int i;
  1162. if (!capable(CAP_MAC_ADMIN))
  1163. return -EPERM;
  1164. if (count >= sizeof(temp) || count == 0)
  1165. return -EINVAL;
  1166. if (copy_from_user(temp, buf, count) != 0)
  1167. return -EFAULT;
  1168. temp[count] = '\0';
  1169. if (sscanf(temp, "%d", &i) != 1)
  1170. return -EINVAL;
  1171. /*
  1172. * Don't do anything if the value hasn't actually changed.
  1173. * If it is changing reset the level on entries that were
  1174. * set up to be direct when they were created.
  1175. */
  1176. if (smack_cipso_direct != i) {
  1177. mutex_lock(&smack_known_lock);
  1178. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1179. if (skp->smk_netlabel.attr.mls.lvl ==
  1180. smack_cipso_direct)
  1181. skp->smk_netlabel.attr.mls.lvl = i;
  1182. smack_cipso_direct = i;
  1183. mutex_unlock(&smack_known_lock);
  1184. }
  1185. return count;
  1186. }
  1187. static const struct file_operations smk_direct_ops = {
  1188. .read = smk_read_direct,
  1189. .write = smk_write_direct,
  1190. .llseek = default_llseek,
  1191. };
  1192. /**
  1193. * smk_read_mapped - read() for /smack/mapped
  1194. * @filp: file pointer, not actually used
  1195. * @buf: where to put the result
  1196. * @count: maximum to send along
  1197. * @ppos: where to start
  1198. *
  1199. * Returns number of bytes read or error code, as appropriate
  1200. */
  1201. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1202. size_t count, loff_t *ppos)
  1203. {
  1204. char temp[80];
  1205. ssize_t rc;
  1206. if (*ppos != 0)
  1207. return 0;
  1208. sprintf(temp, "%d", smack_cipso_mapped);
  1209. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1210. return rc;
  1211. }
  1212. /**
  1213. * smk_write_mapped - write() for /smack/mapped
  1214. * @file: file pointer, not actually used
  1215. * @buf: where to get the data from
  1216. * @count: bytes sent
  1217. * @ppos: where to start
  1218. *
  1219. * Returns number of bytes written or error code, as appropriate
  1220. */
  1221. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1222. size_t count, loff_t *ppos)
  1223. {
  1224. struct smack_known *skp;
  1225. char temp[80];
  1226. int i;
  1227. if (!capable(CAP_MAC_ADMIN))
  1228. return -EPERM;
  1229. if (count >= sizeof(temp) || count == 0)
  1230. return -EINVAL;
  1231. if (copy_from_user(temp, buf, count) != 0)
  1232. return -EFAULT;
  1233. temp[count] = '\0';
  1234. if (sscanf(temp, "%d", &i) != 1)
  1235. return -EINVAL;
  1236. /*
  1237. * Don't do anything if the value hasn't actually changed.
  1238. * If it is changing reset the level on entries that were
  1239. * set up to be mapped when they were created.
  1240. */
  1241. if (smack_cipso_mapped != i) {
  1242. mutex_lock(&smack_known_lock);
  1243. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1244. if (skp->smk_netlabel.attr.mls.lvl ==
  1245. smack_cipso_mapped)
  1246. skp->smk_netlabel.attr.mls.lvl = i;
  1247. smack_cipso_mapped = i;
  1248. mutex_unlock(&smack_known_lock);
  1249. }
  1250. return count;
  1251. }
  1252. static const struct file_operations smk_mapped_ops = {
  1253. .read = smk_read_mapped,
  1254. .write = smk_write_mapped,
  1255. .llseek = default_llseek,
  1256. };
  1257. /**
  1258. * smk_read_ambient - read() for /smack/ambient
  1259. * @filp: file pointer, not actually used
  1260. * @buf: where to put the result
  1261. * @cn: maximum to send along
  1262. * @ppos: where to start
  1263. *
  1264. * Returns number of bytes read or error code, as appropriate
  1265. */
  1266. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1267. size_t cn, loff_t *ppos)
  1268. {
  1269. ssize_t rc;
  1270. int asize;
  1271. if (*ppos != 0)
  1272. return 0;
  1273. /*
  1274. * Being careful to avoid a problem in the case where
  1275. * smack_net_ambient gets changed in midstream.
  1276. */
  1277. mutex_lock(&smack_ambient_lock);
  1278. asize = strlen(smack_net_ambient) + 1;
  1279. if (cn >= asize)
  1280. rc = simple_read_from_buffer(buf, cn, ppos,
  1281. smack_net_ambient, asize);
  1282. else
  1283. rc = -EINVAL;
  1284. mutex_unlock(&smack_ambient_lock);
  1285. return rc;
  1286. }
  1287. /**
  1288. * smk_write_ambient - write() for /smack/ambient
  1289. * @file: file pointer, not actually used
  1290. * @buf: where to get the data from
  1291. * @count: bytes sent
  1292. * @ppos: where to start
  1293. *
  1294. * Returns number of bytes written or error code, as appropriate
  1295. */
  1296. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1297. size_t count, loff_t *ppos)
  1298. {
  1299. char *oldambient;
  1300. char *smack = NULL;
  1301. char *data;
  1302. int rc = count;
  1303. if (!capable(CAP_MAC_ADMIN))
  1304. return -EPERM;
  1305. data = kzalloc(count + 1, GFP_KERNEL);
  1306. if (data == NULL)
  1307. return -ENOMEM;
  1308. if (copy_from_user(data, buf, count) != 0) {
  1309. rc = -EFAULT;
  1310. goto out;
  1311. }
  1312. smack = smk_import(data, count);
  1313. if (smack == NULL) {
  1314. rc = -EINVAL;
  1315. goto out;
  1316. }
  1317. mutex_lock(&smack_ambient_lock);
  1318. oldambient = smack_net_ambient;
  1319. smack_net_ambient = smack;
  1320. smk_unlbl_ambient(oldambient);
  1321. mutex_unlock(&smack_ambient_lock);
  1322. out:
  1323. kfree(data);
  1324. return rc;
  1325. }
  1326. static const struct file_operations smk_ambient_ops = {
  1327. .read = smk_read_ambient,
  1328. .write = smk_write_ambient,
  1329. .llseek = default_llseek,
  1330. };
  1331. /**
  1332. * smk_read_onlycap - read() for /smack/onlycap
  1333. * @filp: file pointer, not actually used
  1334. * @buf: where to put the result
  1335. * @cn: maximum to send along
  1336. * @ppos: where to start
  1337. *
  1338. * Returns number of bytes read or error code, as appropriate
  1339. */
  1340. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  1341. size_t cn, loff_t *ppos)
  1342. {
  1343. char *smack = "";
  1344. ssize_t rc = -EINVAL;
  1345. int asize;
  1346. if (*ppos != 0)
  1347. return 0;
  1348. if (smack_onlycap != NULL)
  1349. smack = smack_onlycap;
  1350. asize = strlen(smack) + 1;
  1351. if (cn >= asize)
  1352. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1353. return rc;
  1354. }
  1355. /**
  1356. * smk_write_onlycap - write() for /smack/onlycap
  1357. * @file: file pointer, not actually used
  1358. * @buf: where to get the data from
  1359. * @count: bytes sent
  1360. * @ppos: where to start
  1361. *
  1362. * Returns number of bytes written or error code, as appropriate
  1363. */
  1364. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1365. size_t count, loff_t *ppos)
  1366. {
  1367. char *data;
  1368. char *sp = smk_of_task(current->cred->security);
  1369. int rc = count;
  1370. if (!capable(CAP_MAC_ADMIN))
  1371. return -EPERM;
  1372. /*
  1373. * This can be done using smk_access() but is done
  1374. * explicitly for clarity. The smk_access() implementation
  1375. * would use smk_access(smack_onlycap, MAY_WRITE)
  1376. */
  1377. if (smack_onlycap != NULL && smack_onlycap != sp)
  1378. return -EPERM;
  1379. data = kzalloc(count, GFP_KERNEL);
  1380. if (data == NULL)
  1381. return -ENOMEM;
  1382. /*
  1383. * Should the null string be passed in unset the onlycap value.
  1384. * This seems like something to be careful with as usually
  1385. * smk_import only expects to return NULL for errors. It
  1386. * is usually the case that a nullstring or "\n" would be
  1387. * bad to pass to smk_import but in fact this is useful here.
  1388. *
  1389. * smk_import will also reject a label beginning with '-',
  1390. * so "-usecapabilities" will also work.
  1391. */
  1392. if (copy_from_user(data, buf, count) != 0)
  1393. rc = -EFAULT;
  1394. else
  1395. smack_onlycap = smk_import(data, count);
  1396. kfree(data);
  1397. return rc;
  1398. }
  1399. static const struct file_operations smk_onlycap_ops = {
  1400. .read = smk_read_onlycap,
  1401. .write = smk_write_onlycap,
  1402. .llseek = default_llseek,
  1403. };
  1404. /**
  1405. * smk_read_logging - read() for /smack/logging
  1406. * @filp: file pointer, not actually used
  1407. * @buf: where to put the result
  1408. * @cn: maximum to send along
  1409. * @ppos: where to start
  1410. *
  1411. * Returns number of bytes read or error code, as appropriate
  1412. */
  1413. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1414. size_t count, loff_t *ppos)
  1415. {
  1416. char temp[32];
  1417. ssize_t rc;
  1418. if (*ppos != 0)
  1419. return 0;
  1420. sprintf(temp, "%d\n", log_policy);
  1421. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1422. return rc;
  1423. }
  1424. /**
  1425. * smk_write_logging - write() for /smack/logging
  1426. * @file: file pointer, not actually used
  1427. * @buf: where to get the data from
  1428. * @count: bytes sent
  1429. * @ppos: where to start
  1430. *
  1431. * Returns number of bytes written or error code, as appropriate
  1432. */
  1433. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1434. size_t count, loff_t *ppos)
  1435. {
  1436. char temp[32];
  1437. int i;
  1438. if (!capable(CAP_MAC_ADMIN))
  1439. return -EPERM;
  1440. if (count >= sizeof(temp) || count == 0)
  1441. return -EINVAL;
  1442. if (copy_from_user(temp, buf, count) != 0)
  1443. return -EFAULT;
  1444. temp[count] = '\0';
  1445. if (sscanf(temp, "%d", &i) != 1)
  1446. return -EINVAL;
  1447. if (i < 0 || i > 3)
  1448. return -EINVAL;
  1449. log_policy = i;
  1450. return count;
  1451. }
  1452. static const struct file_operations smk_logging_ops = {
  1453. .read = smk_read_logging,
  1454. .write = smk_write_logging,
  1455. .llseek = default_llseek,
  1456. };
  1457. /*
  1458. * Seq_file read operations for /smack/load-self
  1459. */
  1460. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1461. {
  1462. struct task_smack *tsp = current_security();
  1463. return smk_seq_start(s, pos, &tsp->smk_rules);
  1464. }
  1465. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1466. {
  1467. struct task_smack *tsp = current_security();
  1468. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1469. }
  1470. static int load_self_seq_show(struct seq_file *s, void *v)
  1471. {
  1472. struct list_head *list = v;
  1473. struct smack_rule *srp =
  1474. list_entry(list, struct smack_rule, list);
  1475. smk_rule_show(s, srp, SMK_LABELLEN);
  1476. return 0;
  1477. }
  1478. static const struct seq_operations load_self_seq_ops = {
  1479. .start = load_self_seq_start,
  1480. .next = load_self_seq_next,
  1481. .show = load_self_seq_show,
  1482. .stop = smk_seq_stop,
  1483. };
  1484. /**
  1485. * smk_open_load_self - open() for /smack/load-self2
  1486. * @inode: inode structure representing file
  1487. * @file: "load" file pointer
  1488. *
  1489. * For reading, use load_seq_* seq_file reading operations.
  1490. */
  1491. static int smk_open_load_self(struct inode *inode, struct file *file)
  1492. {
  1493. return seq_open(file, &load_self_seq_ops);
  1494. }
  1495. /**
  1496. * smk_write_load_self - write() for /smack/load-self
  1497. * @file: file pointer, not actually used
  1498. * @buf: where to get the data from
  1499. * @count: bytes sent
  1500. * @ppos: where to start - must be 0
  1501. *
  1502. */
  1503. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1504. size_t count, loff_t *ppos)
  1505. {
  1506. struct task_smack *tsp = current_security();
  1507. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1508. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1509. }
  1510. static const struct file_operations smk_load_self_ops = {
  1511. .open = smk_open_load_self,
  1512. .read = seq_read,
  1513. .llseek = seq_lseek,
  1514. .write = smk_write_load_self,
  1515. .release = seq_release,
  1516. };
  1517. /**
  1518. * smk_user_access - handle access check transaction
  1519. * @file: file pointer
  1520. * @buf: data from user space
  1521. * @count: bytes sent
  1522. * @ppos: where to start - must be 0
  1523. */
  1524. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1525. size_t count, loff_t *ppos, int format)
  1526. {
  1527. struct smack_rule rule;
  1528. char *data;
  1529. char *cod;
  1530. int res;
  1531. data = simple_transaction_get(file, buf, count);
  1532. if (IS_ERR(data))
  1533. return PTR_ERR(data);
  1534. if (format == SMK_FIXED24_FMT) {
  1535. if (count < SMK_LOADLEN)
  1536. return -EINVAL;
  1537. res = smk_parse_rule(data, &rule, 0);
  1538. } else {
  1539. /*
  1540. * Copy the data to make sure the string is terminated.
  1541. */
  1542. cod = kzalloc(count + 1, GFP_KERNEL);
  1543. if (cod == NULL)
  1544. return -ENOMEM;
  1545. memcpy(cod, data, count);
  1546. cod[count] = '\0';
  1547. res = smk_parse_long_rule(cod, &rule, 0);
  1548. kfree(cod);
  1549. }
  1550. if (res)
  1551. return -EINVAL;
  1552. res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access,
  1553. NULL);
  1554. data[0] = res == 0 ? '1' : '0';
  1555. data[1] = '\0';
  1556. simple_transaction_set(file, 2);
  1557. if (format == SMK_FIXED24_FMT)
  1558. return SMK_LOADLEN;
  1559. return count;
  1560. }
  1561. /**
  1562. * smk_write_access - handle access check transaction
  1563. * @file: file pointer
  1564. * @buf: data from user space
  1565. * @count: bytes sent
  1566. * @ppos: where to start - must be 0
  1567. */
  1568. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  1569. size_t count, loff_t *ppos)
  1570. {
  1571. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  1572. }
  1573. static const struct file_operations smk_access_ops = {
  1574. .write = smk_write_access,
  1575. .read = simple_transaction_read,
  1576. .release = simple_transaction_release,
  1577. .llseek = generic_file_llseek,
  1578. };
  1579. /*
  1580. * Seq_file read operations for /smack/load2
  1581. */
  1582. static int load2_seq_show(struct seq_file *s, void *v)
  1583. {
  1584. struct list_head *list = v;
  1585. struct smack_master_list *smlp =
  1586. list_entry(list, struct smack_master_list, list);
  1587. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  1588. return 0;
  1589. }
  1590. static const struct seq_operations load2_seq_ops = {
  1591. .start = load2_seq_start,
  1592. .next = load2_seq_next,
  1593. .show = load2_seq_show,
  1594. .stop = smk_seq_stop,
  1595. };
  1596. /**
  1597. * smk_open_load2 - open() for /smack/load2
  1598. * @inode: inode structure representing file
  1599. * @file: "load2" file pointer
  1600. *
  1601. * For reading, use load2_seq_* seq_file reading operations.
  1602. */
  1603. static int smk_open_load2(struct inode *inode, struct file *file)
  1604. {
  1605. return seq_open(file, &load2_seq_ops);
  1606. }
  1607. /**
  1608. * smk_write_load2 - write() for /smack/load2
  1609. * @file: file pointer, not actually used
  1610. * @buf: where to get the data from
  1611. * @count: bytes sent
  1612. * @ppos: where to start - must be 0
  1613. *
  1614. */
  1615. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  1616. size_t count, loff_t *ppos)
  1617. {
  1618. /*
  1619. * Must have privilege.
  1620. */
  1621. if (!capable(CAP_MAC_ADMIN))
  1622. return -EPERM;
  1623. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  1624. SMK_LONG_FMT);
  1625. }
  1626. static const struct file_operations smk_load2_ops = {
  1627. .open = smk_open_load2,
  1628. .read = seq_read,
  1629. .llseek = seq_lseek,
  1630. .write = smk_write_load2,
  1631. .release = seq_release,
  1632. };
  1633. /*
  1634. * Seq_file read operations for /smack/load-self2
  1635. */
  1636. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  1637. {
  1638. struct task_smack *tsp = current_security();
  1639. return smk_seq_start(s, pos, &tsp->smk_rules);
  1640. }
  1641. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1642. {
  1643. struct task_smack *tsp = current_security();
  1644. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1645. }
  1646. static int load_self2_seq_show(struct seq_file *s, void *v)
  1647. {
  1648. struct list_head *list = v;
  1649. struct smack_rule *srp =
  1650. list_entry(list, struct smack_rule, list);
  1651. smk_rule_show(s, srp, SMK_LONGLABEL);
  1652. return 0;
  1653. }
  1654. static const struct seq_operations load_self2_seq_ops = {
  1655. .start = load_self2_seq_start,
  1656. .next = load_self2_seq_next,
  1657. .show = load_self2_seq_show,
  1658. .stop = smk_seq_stop,
  1659. };
  1660. /**
  1661. * smk_open_load_self2 - open() for /smack/load-self2
  1662. * @inode: inode structure representing file
  1663. * @file: "load" file pointer
  1664. *
  1665. * For reading, use load_seq_* seq_file reading operations.
  1666. */
  1667. static int smk_open_load_self2(struct inode *inode, struct file *file)
  1668. {
  1669. return seq_open(file, &load_self2_seq_ops);
  1670. }
  1671. /**
  1672. * smk_write_load_self2 - write() for /smack/load-self2
  1673. * @file: file pointer, not actually used
  1674. * @buf: where to get the data from
  1675. * @count: bytes sent
  1676. * @ppos: where to start - must be 0
  1677. *
  1678. */
  1679. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  1680. size_t count, loff_t *ppos)
  1681. {
  1682. struct task_smack *tsp = current_security();
  1683. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1684. &tsp->smk_rules_lock, SMK_LONG_FMT);
  1685. }
  1686. static const struct file_operations smk_load_self2_ops = {
  1687. .open = smk_open_load_self2,
  1688. .read = seq_read,
  1689. .llseek = seq_lseek,
  1690. .write = smk_write_load_self2,
  1691. .release = seq_release,
  1692. };
  1693. /**
  1694. * smk_write_access2 - handle access check transaction
  1695. * @file: file pointer
  1696. * @buf: data from user space
  1697. * @count: bytes sent
  1698. * @ppos: where to start - must be 0
  1699. */
  1700. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  1701. size_t count, loff_t *ppos)
  1702. {
  1703. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  1704. }
  1705. static const struct file_operations smk_access2_ops = {
  1706. .write = smk_write_access2,
  1707. .read = simple_transaction_read,
  1708. .release = simple_transaction_release,
  1709. .llseek = generic_file_llseek,
  1710. };
  1711. /**
  1712. * smk_fill_super - fill the /smackfs superblock
  1713. * @sb: the empty superblock
  1714. * @data: unused
  1715. * @silent: unused
  1716. *
  1717. * Fill in the well known entries for /smack
  1718. *
  1719. * Returns 0 on success, an error code on failure
  1720. */
  1721. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  1722. {
  1723. int rc;
  1724. struct inode *root_inode;
  1725. static struct tree_descr smack_files[] = {
  1726. [SMK_LOAD] = {
  1727. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  1728. [SMK_CIPSO] = {
  1729. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  1730. [SMK_DOI] = {
  1731. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  1732. [SMK_DIRECT] = {
  1733. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  1734. [SMK_AMBIENT] = {
  1735. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  1736. [SMK_NETLBLADDR] = {
  1737. "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  1738. [SMK_ONLYCAP] = {
  1739. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  1740. [SMK_LOGGING] = {
  1741. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  1742. [SMK_LOAD_SELF] = {
  1743. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  1744. [SMK_ACCESSES] = {
  1745. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  1746. [SMK_MAPPED] = {
  1747. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  1748. [SMK_LOAD2] = {
  1749. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  1750. [SMK_LOAD_SELF2] = {
  1751. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  1752. [SMK_ACCESS2] = {
  1753. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  1754. [SMK_CIPSO2] = {
  1755. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  1756. /* last one */
  1757. {""}
  1758. };
  1759. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  1760. if (rc != 0) {
  1761. printk(KERN_ERR "%s failed %d while creating inodes\n",
  1762. __func__, rc);
  1763. return rc;
  1764. }
  1765. root_inode = sb->s_root->d_inode;
  1766. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  1767. return 0;
  1768. }
  1769. /**
  1770. * smk_mount - get the smackfs superblock
  1771. * @fs_type: passed along without comment
  1772. * @flags: passed along without comment
  1773. * @dev_name: passed along without comment
  1774. * @data: passed along without comment
  1775. *
  1776. * Just passes everything along.
  1777. *
  1778. * Returns what the lower level code does.
  1779. */
  1780. static struct dentry *smk_mount(struct file_system_type *fs_type,
  1781. int flags, const char *dev_name, void *data)
  1782. {
  1783. return mount_single(fs_type, flags, data, smk_fill_super);
  1784. }
  1785. static struct file_system_type smk_fs_type = {
  1786. .name = "smackfs",
  1787. .mount = smk_mount,
  1788. .kill_sb = kill_litter_super,
  1789. };
  1790. static struct vfsmount *smackfs_mount;
  1791. static int __init smk_preset_netlabel(struct smack_known *skp)
  1792. {
  1793. skp->smk_netlabel.domain = skp->smk_known;
  1794. skp->smk_netlabel.flags =
  1795. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  1796. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  1797. &skp->smk_netlabel, strlen(skp->smk_known));
  1798. }
  1799. /**
  1800. * init_smk_fs - get the smackfs superblock
  1801. *
  1802. * register the smackfs
  1803. *
  1804. * Do not register smackfs if Smack wasn't enabled
  1805. * on boot. We can not put this method normally under the
  1806. * smack_init() code path since the security subsystem get
  1807. * initialized before the vfs caches.
  1808. *
  1809. * Returns true if we were not chosen on boot or if
  1810. * we were chosen and filesystem registration succeeded.
  1811. */
  1812. static int __init init_smk_fs(void)
  1813. {
  1814. int err;
  1815. int rc;
  1816. if (!security_module_enable(&smack_ops))
  1817. return 0;
  1818. err = register_filesystem(&smk_fs_type);
  1819. if (!err) {
  1820. smackfs_mount = kern_mount(&smk_fs_type);
  1821. if (IS_ERR(smackfs_mount)) {
  1822. printk(KERN_ERR "smackfs: could not mount!\n");
  1823. err = PTR_ERR(smackfs_mount);
  1824. smackfs_mount = NULL;
  1825. }
  1826. }
  1827. smk_cipso_doi();
  1828. smk_unlbl_ambient(NULL);
  1829. rc = smk_preset_netlabel(&smack_known_floor);
  1830. if (err == 0 && rc < 0)
  1831. err = rc;
  1832. rc = smk_preset_netlabel(&smack_known_hat);
  1833. if (err == 0 && rc < 0)
  1834. err = rc;
  1835. rc = smk_preset_netlabel(&smack_known_huh);
  1836. if (err == 0 && rc < 0)
  1837. err = rc;
  1838. rc = smk_preset_netlabel(&smack_known_invalid);
  1839. if (err == 0 && rc < 0)
  1840. err = rc;
  1841. rc = smk_preset_netlabel(&smack_known_star);
  1842. if (err == 0 && rc < 0)
  1843. err = rc;
  1844. rc = smk_preset_netlabel(&smack_known_web);
  1845. if (err == 0 && rc < 0)
  1846. err = rc;
  1847. return err;
  1848. }
  1849. __initcall(init_smk_fs);