smackfs.c 69 KB

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