smackfs.c 70 KB

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