smackfs.c 52 KB

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