smackfs.c 60 KB

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