commoncap.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. /* Common capabilities, needed by capability.o.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/audit.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/lsm_hooks.h>
  15. #include <linux/file.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/swap.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netlink.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/xattr.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/mount.h>
  26. #include <linux/sched.h>
  27. #include <linux/prctl.h>
  28. #include <linux/securebits.h>
  29. #include <linux/user_namespace.h>
  30. #include <linux/binfmts.h>
  31. #include <linux/personality.h>
  32. /*
  33. * If a non-root user executes a setuid-root binary in
  34. * !secure(SECURE_NOROOT) mode, then we raise capabilities.
  35. * However if fE is also set, then the intent is for only
  36. * the file capabilities to be applied, and the setuid-root
  37. * bit is left on either to change the uid (plausible) or
  38. * to get full privilege on a kernel without file capabilities
  39. * support. So in that case we do not raise capabilities.
  40. *
  41. * Warn if that happens, once per boot.
  42. */
  43. static void warn_setuid_and_fcaps_mixed(const char *fname)
  44. {
  45. static int warned;
  46. if (!warned) {
  47. printk(KERN_INFO "warning: `%s' has both setuid-root and"
  48. " effective capabilities. Therefore not raising all"
  49. " capabilities.\n", fname);
  50. warned = 1;
  51. }
  52. }
  53. /**
  54. * cap_capable - Determine whether a task has a particular effective capability
  55. * @cred: The credentials to use
  56. * @ns: The user namespace in which we need the capability
  57. * @cap: The capability to check for
  58. * @audit: Whether to write an audit message or not
  59. *
  60. * Determine whether the nominated task has the specified capability amongst
  61. * its effective set, returning 0 if it does, -ve if it does not.
  62. *
  63. * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
  64. * and has_capability() functions. That is, it has the reverse semantics:
  65. * cap_has_capability() returns 0 when a task has a capability, but the
  66. * kernel's capable() and has_capability() returns 1 for this case.
  67. */
  68. int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
  69. int cap, unsigned int opts)
  70. {
  71. struct user_namespace *ns = targ_ns;
  72. /* See if cred has the capability in the target user namespace
  73. * by examining the target user namespace and all of the target
  74. * user namespace's parents.
  75. */
  76. for (;;) {
  77. /* Do we have the necessary capabilities? */
  78. if (ns == cred->user_ns)
  79. return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
  80. /*
  81. * If we're already at a lower level than we're looking for,
  82. * we're done searching.
  83. */
  84. if (ns->level <= cred->user_ns->level)
  85. return -EPERM;
  86. /*
  87. * The owner of the user namespace in the parent of the
  88. * user namespace has all caps.
  89. */
  90. if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
  91. return 0;
  92. /*
  93. * If you have a capability in a parent user ns, then you have
  94. * it over all children user namespaces as well.
  95. */
  96. ns = ns->parent;
  97. }
  98. /* We never get here */
  99. }
  100. /**
  101. * cap_settime - Determine whether the current process may set the system clock
  102. * @ts: The time to set
  103. * @tz: The timezone to set
  104. *
  105. * Determine whether the current process may set the system clock and timezone
  106. * information, returning 0 if permission granted, -ve if denied.
  107. */
  108. int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
  109. {
  110. if (!capable(CAP_SYS_TIME))
  111. return -EPERM;
  112. return 0;
  113. }
  114. /**
  115. * cap_ptrace_access_check - Determine whether the current process may access
  116. * another
  117. * @child: The process to be accessed
  118. * @mode: The mode of attachment.
  119. *
  120. * If we are in the same or an ancestor user_ns and have all the target
  121. * task's capabilities, then ptrace access is allowed.
  122. * If we have the ptrace capability to the target user_ns, then ptrace
  123. * access is allowed.
  124. * Else denied.
  125. *
  126. * Determine whether a process may access another, returning 0 if permission
  127. * granted, -ve if denied.
  128. */
  129. int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
  130. {
  131. int ret = 0;
  132. const struct cred *cred, *child_cred;
  133. const kernel_cap_t *caller_caps;
  134. rcu_read_lock();
  135. cred = current_cred();
  136. child_cred = __task_cred(child);
  137. if (mode & PTRACE_MODE_FSCREDS)
  138. caller_caps = &cred->cap_effective;
  139. else
  140. caller_caps = &cred->cap_permitted;
  141. if (cred->user_ns == child_cred->user_ns &&
  142. cap_issubset(child_cred->cap_permitted, *caller_caps))
  143. goto out;
  144. if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
  145. goto out;
  146. ret = -EPERM;
  147. out:
  148. rcu_read_unlock();
  149. return ret;
  150. }
  151. /**
  152. * cap_ptrace_traceme - Determine whether another process may trace the current
  153. * @parent: The task proposed to be the tracer
  154. *
  155. * If parent is in the same or an ancestor user_ns and has all current's
  156. * capabilities, then ptrace access is allowed.
  157. * If parent has the ptrace capability to current's user_ns, then ptrace
  158. * access is allowed.
  159. * Else denied.
  160. *
  161. * Determine whether the nominated task is permitted to trace the current
  162. * process, returning 0 if permission is granted, -ve if denied.
  163. */
  164. int cap_ptrace_traceme(struct task_struct *parent)
  165. {
  166. int ret = 0;
  167. const struct cred *cred, *child_cred;
  168. rcu_read_lock();
  169. cred = __task_cred(parent);
  170. child_cred = current_cred();
  171. if (cred->user_ns == child_cred->user_ns &&
  172. cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
  173. goto out;
  174. if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
  175. goto out;
  176. ret = -EPERM;
  177. out:
  178. rcu_read_unlock();
  179. return ret;
  180. }
  181. /**
  182. * cap_capget - Retrieve a task's capability sets
  183. * @target: The task from which to retrieve the capability sets
  184. * @effective: The place to record the effective set
  185. * @inheritable: The place to record the inheritable set
  186. * @permitted: The place to record the permitted set
  187. *
  188. * This function retrieves the capabilities of the nominated task and returns
  189. * them to the caller.
  190. */
  191. int cap_capget(struct task_struct *target, kernel_cap_t *effective,
  192. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  193. {
  194. const struct cred *cred;
  195. /* Derived from kernel/capability.c:sys_capget. */
  196. rcu_read_lock();
  197. cred = __task_cred(target);
  198. *effective = cred->cap_effective;
  199. *inheritable = cred->cap_inheritable;
  200. *permitted = cred->cap_permitted;
  201. rcu_read_unlock();
  202. return 0;
  203. }
  204. /*
  205. * Determine whether the inheritable capabilities are limited to the old
  206. * permitted set. Returns 1 if they are limited, 0 if they are not.
  207. */
  208. static inline int cap_inh_is_capped(void)
  209. {
  210. /* they are so limited unless the current task has the CAP_SETPCAP
  211. * capability
  212. */
  213. if (cap_capable(current_cred(), current_cred()->user_ns,
  214. CAP_SETPCAP, CAP_OPT_NONE) == 0)
  215. return 0;
  216. return 1;
  217. }
  218. /**
  219. * cap_capset - Validate and apply proposed changes to current's capabilities
  220. * @new: The proposed new credentials; alterations should be made here
  221. * @old: The current task's current credentials
  222. * @effective: A pointer to the proposed new effective capabilities set
  223. * @inheritable: A pointer to the proposed new inheritable capabilities set
  224. * @permitted: A pointer to the proposed new permitted capabilities set
  225. *
  226. * This function validates and applies a proposed mass change to the current
  227. * process's capability sets. The changes are made to the proposed new
  228. * credentials, and assuming no error, will be committed by the caller of LSM.
  229. */
  230. int cap_capset(struct cred *new,
  231. const struct cred *old,
  232. const kernel_cap_t *effective,
  233. const kernel_cap_t *inheritable,
  234. const kernel_cap_t *permitted)
  235. {
  236. if (cap_inh_is_capped() &&
  237. !cap_issubset(*inheritable,
  238. cap_combine(old->cap_inheritable,
  239. old->cap_permitted)))
  240. /* incapable of using this inheritable set */
  241. return -EPERM;
  242. if (!cap_issubset(*inheritable,
  243. cap_combine(old->cap_inheritable,
  244. old->cap_bset)))
  245. /* no new pI capabilities outside bounding set */
  246. return -EPERM;
  247. /* verify restrictions on target's new Permitted set */
  248. if (!cap_issubset(*permitted, old->cap_permitted))
  249. return -EPERM;
  250. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  251. if (!cap_issubset(*effective, *permitted))
  252. return -EPERM;
  253. new->cap_effective = *effective;
  254. new->cap_inheritable = *inheritable;
  255. new->cap_permitted = *permitted;
  256. /*
  257. * Mask off ambient bits that are no longer both permitted and
  258. * inheritable.
  259. */
  260. new->cap_ambient = cap_intersect(new->cap_ambient,
  261. cap_intersect(*permitted,
  262. *inheritable));
  263. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  264. return -EINVAL;
  265. return 0;
  266. }
  267. /**
  268. * cap_inode_need_killpriv - Determine if inode change affects privileges
  269. * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
  270. *
  271. * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
  272. * affects the security markings on that inode, and if it is, should
  273. * inode_killpriv() be invoked or the change rejected.
  274. *
  275. * Returns 1 if security.capability has a value, meaning inode_killpriv()
  276. * is required, 0 otherwise, meaning inode_killpriv() is not required.
  277. */
  278. int cap_inode_need_killpriv(struct dentry *dentry)
  279. {
  280. struct inode *inode = d_backing_inode(dentry);
  281. int error;
  282. error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0);
  283. return error > 0;
  284. }
  285. /**
  286. * cap_inode_killpriv - Erase the security markings on an inode
  287. * @dentry: The inode/dentry to alter
  288. *
  289. * Erase the privilege-enhancing security markings on an inode.
  290. *
  291. * Returns 0 if successful, -ve on error.
  292. */
  293. int cap_inode_killpriv(struct dentry *dentry)
  294. {
  295. int error;
  296. error = __vfs_removexattr(dentry, XATTR_NAME_CAPS);
  297. if (error == -EOPNOTSUPP)
  298. error = 0;
  299. return error;
  300. }
  301. static bool rootid_owns_currentns(kuid_t kroot)
  302. {
  303. struct user_namespace *ns;
  304. if (!uid_valid(kroot))
  305. return false;
  306. for (ns = current_user_ns(); ; ns = ns->parent) {
  307. if (from_kuid(ns, kroot) == 0)
  308. return true;
  309. if (ns == &init_user_ns)
  310. break;
  311. }
  312. return false;
  313. }
  314. static __u32 sansflags(__u32 m)
  315. {
  316. return m & ~VFS_CAP_FLAGS_EFFECTIVE;
  317. }
  318. static bool is_v2header(size_t size, const struct vfs_cap_data *cap)
  319. {
  320. if (size != XATTR_CAPS_SZ_2)
  321. return false;
  322. return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_2;
  323. }
  324. static bool is_v3header(size_t size, const struct vfs_cap_data *cap)
  325. {
  326. if (size != XATTR_CAPS_SZ_3)
  327. return false;
  328. return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_3;
  329. }
  330. /*
  331. * getsecurity: We are called for security.* before any attempt to read the
  332. * xattr from the inode itself.
  333. *
  334. * This gives us a chance to read the on-disk value and convert it. If we
  335. * return -EOPNOTSUPP, then vfs_getxattr() will call the i_op handler.
  336. *
  337. * Note we are not called by vfs_getxattr_alloc(), but that is only called
  338. * by the integrity subsystem, which really wants the unconverted values -
  339. * so that's good.
  340. */
  341. int cap_inode_getsecurity(struct inode *inode, const char *name, void **buffer,
  342. bool alloc)
  343. {
  344. int size, ret;
  345. kuid_t kroot;
  346. uid_t root, mappedroot;
  347. char *tmpbuf = NULL;
  348. struct vfs_cap_data *cap;
  349. struct vfs_ns_cap_data *nscap;
  350. struct dentry *dentry;
  351. struct user_namespace *fs_ns;
  352. if (strcmp(name, "capability") != 0)
  353. return -EOPNOTSUPP;
  354. dentry = d_find_any_alias(inode);
  355. if (!dentry)
  356. return -EINVAL;
  357. size = sizeof(struct vfs_ns_cap_data);
  358. ret = (int) vfs_getxattr_alloc(dentry, XATTR_NAME_CAPS,
  359. &tmpbuf, size, GFP_NOFS);
  360. dput(dentry);
  361. if (ret < 0)
  362. return ret;
  363. fs_ns = inode->i_sb->s_user_ns;
  364. cap = (struct vfs_cap_data *) tmpbuf;
  365. if (is_v2header((size_t) ret, cap)) {
  366. /* If this is sizeof(vfs_cap_data) then we're ok with the
  367. * on-disk value, so return that. */
  368. if (alloc)
  369. *buffer = tmpbuf;
  370. else
  371. kfree(tmpbuf);
  372. return ret;
  373. } else if (!is_v3header((size_t) ret, cap)) {
  374. kfree(tmpbuf);
  375. return -EINVAL;
  376. }
  377. nscap = (struct vfs_ns_cap_data *) tmpbuf;
  378. root = le32_to_cpu(nscap->rootid);
  379. kroot = make_kuid(fs_ns, root);
  380. /* If the root kuid maps to a valid uid in current ns, then return
  381. * this as a nscap. */
  382. mappedroot = from_kuid(current_user_ns(), kroot);
  383. if (mappedroot != (uid_t)-1 && mappedroot != (uid_t)0) {
  384. if (alloc) {
  385. *buffer = tmpbuf;
  386. nscap->rootid = cpu_to_le32(mappedroot);
  387. } else
  388. kfree(tmpbuf);
  389. return size;
  390. }
  391. if (!rootid_owns_currentns(kroot)) {
  392. kfree(tmpbuf);
  393. return -EOPNOTSUPP;
  394. }
  395. /* This comes from a parent namespace. Return as a v2 capability */
  396. size = sizeof(struct vfs_cap_data);
  397. if (alloc) {
  398. *buffer = kmalloc(size, GFP_ATOMIC);
  399. if (*buffer) {
  400. struct vfs_cap_data *cap = *buffer;
  401. __le32 nsmagic, magic;
  402. magic = VFS_CAP_REVISION_2;
  403. nsmagic = le32_to_cpu(nscap->magic_etc);
  404. if (nsmagic & VFS_CAP_FLAGS_EFFECTIVE)
  405. magic |= VFS_CAP_FLAGS_EFFECTIVE;
  406. memcpy(&cap->data, &nscap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
  407. cap->magic_etc = cpu_to_le32(magic);
  408. } else {
  409. size = -ENOMEM;
  410. }
  411. }
  412. kfree(tmpbuf);
  413. return size;
  414. }
  415. static kuid_t rootid_from_xattr(const void *value, size_t size,
  416. struct user_namespace *task_ns)
  417. {
  418. const struct vfs_ns_cap_data *nscap = value;
  419. uid_t rootid = 0;
  420. if (size == XATTR_CAPS_SZ_3)
  421. rootid = le32_to_cpu(nscap->rootid);
  422. return make_kuid(task_ns, rootid);
  423. }
  424. static bool validheader(size_t size, const struct vfs_cap_data *cap)
  425. {
  426. return is_v2header(size, cap) || is_v3header(size, cap);
  427. }
  428. /*
  429. * User requested a write of security.capability. If needed, update the
  430. * xattr to change from v2 to v3, or to fixup the v3 rootid.
  431. *
  432. * If all is ok, we return the new size, on error return < 0.
  433. */
  434. int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
  435. {
  436. struct vfs_ns_cap_data *nscap;
  437. uid_t nsrootid;
  438. const struct vfs_cap_data *cap = *ivalue;
  439. __u32 magic, nsmagic;
  440. struct inode *inode = d_backing_inode(dentry);
  441. struct user_namespace *task_ns = current_user_ns(),
  442. *fs_ns = inode->i_sb->s_user_ns;
  443. kuid_t rootid;
  444. size_t newsize;
  445. if (!*ivalue)
  446. return -EINVAL;
  447. if (!validheader(size, cap))
  448. return -EINVAL;
  449. if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
  450. return -EPERM;
  451. if (size == XATTR_CAPS_SZ_2)
  452. if (ns_capable(inode->i_sb->s_user_ns, CAP_SETFCAP))
  453. /* user is privileged, just write the v2 */
  454. return size;
  455. rootid = rootid_from_xattr(*ivalue, size, task_ns);
  456. if (!uid_valid(rootid))
  457. return -EINVAL;
  458. nsrootid = from_kuid(fs_ns, rootid);
  459. if (nsrootid == -1)
  460. return -EINVAL;
  461. newsize = sizeof(struct vfs_ns_cap_data);
  462. nscap = kmalloc(newsize, GFP_ATOMIC);
  463. if (!nscap)
  464. return -ENOMEM;
  465. nscap->rootid = cpu_to_le32(nsrootid);
  466. nsmagic = VFS_CAP_REVISION_3;
  467. magic = le32_to_cpu(cap->magic_etc);
  468. if (magic & VFS_CAP_FLAGS_EFFECTIVE)
  469. nsmagic |= VFS_CAP_FLAGS_EFFECTIVE;
  470. nscap->magic_etc = cpu_to_le32(nsmagic);
  471. memcpy(&nscap->data, &cap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
  472. kvfree(*ivalue);
  473. *ivalue = nscap;
  474. return newsize;
  475. }
  476. /*
  477. * Calculate the new process capability sets from the capability sets attached
  478. * to a file.
  479. */
  480. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  481. struct linux_binprm *bprm,
  482. bool *effective,
  483. bool *has_fcap)
  484. {
  485. struct cred *new = bprm->cred;
  486. unsigned i;
  487. int ret = 0;
  488. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  489. *effective = true;
  490. if (caps->magic_etc & VFS_CAP_REVISION_MASK)
  491. *has_fcap = true;
  492. CAP_FOR_EACH_U32(i) {
  493. __u32 permitted = caps->permitted.cap[i];
  494. __u32 inheritable = caps->inheritable.cap[i];
  495. /*
  496. * pP' = (X & fP) | (pI & fI)
  497. * The addition of pA' is handled later.
  498. */
  499. new->cap_permitted.cap[i] =
  500. (new->cap_bset.cap[i] & permitted) |
  501. (new->cap_inheritable.cap[i] & inheritable);
  502. if (permitted & ~new->cap_permitted.cap[i])
  503. /* insufficient to execute correctly */
  504. ret = -EPERM;
  505. }
  506. /*
  507. * For legacy apps, with no internal support for recognizing they
  508. * do not have enough capabilities, we return an error if they are
  509. * missing some "forced" (aka file-permitted) capabilities.
  510. */
  511. return *effective ? ret : 0;
  512. }
  513. /*
  514. * Extract the on-exec-apply capability sets for an executable file.
  515. */
  516. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  517. {
  518. struct inode *inode = d_backing_inode(dentry);
  519. __u32 magic_etc;
  520. unsigned tocopy, i;
  521. int size;
  522. struct vfs_ns_cap_data data, *nscaps = &data;
  523. struct vfs_cap_data *caps = (struct vfs_cap_data *) &data;
  524. kuid_t rootkuid;
  525. struct user_namespace *fs_ns;
  526. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  527. if (!inode)
  528. return -ENODATA;
  529. fs_ns = inode->i_sb->s_user_ns;
  530. size = __vfs_getxattr((struct dentry *)dentry, inode,
  531. XATTR_NAME_CAPS, &data, XATTR_CAPS_SZ);
  532. if (size == -ENODATA || size == -EOPNOTSUPP)
  533. /* no data, that's ok */
  534. return -ENODATA;
  535. if (size < 0)
  536. return size;
  537. if (size < sizeof(magic_etc))
  538. return -EINVAL;
  539. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps->magic_etc);
  540. rootkuid = make_kuid(fs_ns, 0);
  541. switch (magic_etc & VFS_CAP_REVISION_MASK) {
  542. case VFS_CAP_REVISION_1:
  543. if (size != XATTR_CAPS_SZ_1)
  544. return -EINVAL;
  545. tocopy = VFS_CAP_U32_1;
  546. break;
  547. case VFS_CAP_REVISION_2:
  548. if (size != XATTR_CAPS_SZ_2)
  549. return -EINVAL;
  550. tocopy = VFS_CAP_U32_2;
  551. break;
  552. case VFS_CAP_REVISION_3:
  553. if (size != XATTR_CAPS_SZ_3)
  554. return -EINVAL;
  555. tocopy = VFS_CAP_U32_3;
  556. rootkuid = make_kuid(fs_ns, le32_to_cpu(nscaps->rootid));
  557. break;
  558. default:
  559. return -EINVAL;
  560. }
  561. /* Limit the caps to the mounter of the filesystem
  562. * or the more limited uid specified in the xattr.
  563. */
  564. if (!rootid_owns_currentns(rootkuid))
  565. return -ENODATA;
  566. CAP_FOR_EACH_U32(i) {
  567. if (i >= tocopy)
  568. break;
  569. cpu_caps->permitted.cap[i] = le32_to_cpu(caps->data[i].permitted);
  570. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps->data[i].inheritable);
  571. }
  572. cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  573. cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  574. return 0;
  575. }
  576. /*
  577. * Attempt to get the on-exec apply capability sets for an executable file from
  578. * its xattrs and, if present, apply them to the proposed credentials being
  579. * constructed by execve().
  580. */
  581. static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_fcap)
  582. {
  583. int rc = 0;
  584. struct cpu_vfs_cap_data vcaps;
  585. cap_clear(bprm->cred->cap_permitted);
  586. if (!file_caps_enabled)
  587. return 0;
  588. if (!mnt_may_suid(bprm->file->f_path.mnt))
  589. return 0;
  590. /*
  591. * This check is redundant with mnt_may_suid() but is kept to make
  592. * explicit that capability bits are limited to s_user_ns and its
  593. * descendants.
  594. */
  595. if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
  596. return 0;
  597. rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
  598. if (rc < 0) {
  599. if (rc == -EINVAL)
  600. printk(KERN_NOTICE "Invalid argument reading file caps for %s\n",
  601. bprm->filename);
  602. else if (rc == -ENODATA)
  603. rc = 0;
  604. goto out;
  605. }
  606. rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_fcap);
  607. if (rc == -EINVAL)
  608. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  609. __func__, rc, bprm->filename);
  610. out:
  611. if (rc)
  612. cap_clear(bprm->cred->cap_permitted);
  613. return rc;
  614. }
  615. static inline bool root_privileged(void) { return !issecure(SECURE_NOROOT); }
  616. static inline bool __is_real(kuid_t uid, struct cred *cred)
  617. { return uid_eq(cred->uid, uid); }
  618. static inline bool __is_eff(kuid_t uid, struct cred *cred)
  619. { return uid_eq(cred->euid, uid); }
  620. static inline bool __is_suid(kuid_t uid, struct cred *cred)
  621. { return !__is_real(uid, cred) && __is_eff(uid, cred); }
  622. /*
  623. * handle_privileged_root - Handle case of privileged root
  624. * @bprm: The execution parameters, including the proposed creds
  625. * @has_fcap: Are any file capabilities set?
  626. * @effective: Do we have effective root privilege?
  627. * @root_uid: This namespace' root UID WRT initial USER namespace
  628. *
  629. * Handle the case where root is privileged and hasn't been neutered by
  630. * SECURE_NOROOT. If file capabilities are set, they won't be combined with
  631. * set UID root and nothing is changed. If we are root, cap_permitted is
  632. * updated. If we have become set UID root, the effective bit is set.
  633. */
  634. static void handle_privileged_root(struct linux_binprm *bprm, bool has_fcap,
  635. bool *effective, kuid_t root_uid)
  636. {
  637. const struct cred *old = current_cred();
  638. struct cred *new = bprm->cred;
  639. if (!root_privileged())
  640. return;
  641. /*
  642. * If the legacy file capability is set, then don't set privs
  643. * for a setuid root binary run by a non-root user. Do set it
  644. * for a root user just to cause least surprise to an admin.
  645. */
  646. if (has_fcap && __is_suid(root_uid, new)) {
  647. warn_setuid_and_fcaps_mixed(bprm->filename);
  648. return;
  649. }
  650. /*
  651. * To support inheritance of root-permissions and suid-root
  652. * executables under compatibility mode, we override the
  653. * capability sets for the file.
  654. */
  655. if (__is_eff(root_uid, new) || __is_real(root_uid, new)) {
  656. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  657. new->cap_permitted = cap_combine(old->cap_bset,
  658. old->cap_inheritable);
  659. }
  660. /*
  661. * If only the real uid is 0, we do not set the effective bit.
  662. */
  663. if (__is_eff(root_uid, new))
  664. *effective = true;
  665. }
  666. #define __cap_gained(field, target, source) \
  667. !cap_issubset(target->cap_##field, source->cap_##field)
  668. #define __cap_grew(target, source, cred) \
  669. !cap_issubset(cred->cap_##target, cred->cap_##source)
  670. #define __cap_full(field, cred) \
  671. cap_issubset(CAP_FULL_SET, cred->cap_##field)
  672. static inline bool __is_setuid(struct cred *new, const struct cred *old)
  673. { return !uid_eq(new->euid, old->uid); }
  674. static inline bool __is_setgid(struct cred *new, const struct cred *old)
  675. { return !gid_eq(new->egid, old->gid); }
  676. /*
  677. * 1) Audit candidate if current->cap_effective is set
  678. *
  679. * We do not bother to audit if 3 things are true:
  680. * 1) cap_effective has all caps
  681. * 2) we became root *OR* are were already root
  682. * 3) root is supposed to have all caps (SECURE_NOROOT)
  683. * Since this is just a normal root execing a process.
  684. *
  685. * Number 1 above might fail if you don't have a full bset, but I think
  686. * that is interesting information to audit.
  687. *
  688. * A number of other conditions require logging:
  689. * 2) something prevented setuid root getting all caps
  690. * 3) non-setuid root gets fcaps
  691. * 4) non-setuid root gets ambient
  692. */
  693. static inline bool nonroot_raised_pE(struct cred *new, const struct cred *old,
  694. kuid_t root, bool has_fcap)
  695. {
  696. bool ret = false;
  697. if ((__cap_grew(effective, ambient, new) &&
  698. !(__cap_full(effective, new) &&
  699. (__is_eff(root, new) || __is_real(root, new)) &&
  700. root_privileged())) ||
  701. (root_privileged() &&
  702. __is_suid(root, new) &&
  703. !__cap_full(effective, new)) ||
  704. (!__is_setuid(new, old) &&
  705. ((has_fcap &&
  706. __cap_gained(permitted, new, old)) ||
  707. __cap_gained(ambient, new, old))))
  708. ret = true;
  709. return ret;
  710. }
  711. /**
  712. * cap_bprm_set_creds - Set up the proposed credentials for execve().
  713. * @bprm: The execution parameters, including the proposed creds
  714. *
  715. * Set up the proposed credentials for a new execution context being
  716. * constructed by execve(). The proposed creds in @bprm->cred is altered,
  717. * which won't take effect immediately. Returns 0 if successful, -ve on error.
  718. */
  719. int cap_bprm_set_creds(struct linux_binprm *bprm)
  720. {
  721. const struct cred *old = current_cred();
  722. struct cred *new = bprm->cred;
  723. bool effective = false, has_fcap = false, is_setid;
  724. int ret;
  725. kuid_t root_uid;
  726. if (WARN_ON(!cap_ambient_invariant_ok(old)))
  727. return -EPERM;
  728. ret = get_file_caps(bprm, &effective, &has_fcap);
  729. if (ret < 0)
  730. return ret;
  731. root_uid = make_kuid(new->user_ns, 0);
  732. handle_privileged_root(bprm, has_fcap, &effective, root_uid);
  733. /* if we have fs caps, clear dangerous personality flags */
  734. if (__cap_gained(permitted, new, old))
  735. bprm->per_clear |= PER_CLEAR_ON_SETID;
  736. /* Don't let someone trace a set[ug]id/setpcap binary with the revised
  737. * credentials unless they have the appropriate permit.
  738. *
  739. * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
  740. */
  741. is_setid = __is_setuid(new, old) || __is_setgid(new, old);
  742. if ((is_setid || __cap_gained(permitted, new, old)) &&
  743. ((bprm->unsafe & ~LSM_UNSAFE_PTRACE) ||
  744. !ptracer_capable(current, new->user_ns))) {
  745. /* downgrade; they get no more than they had, and maybe less */
  746. if (!ns_capable(new->user_ns, CAP_SETUID) ||
  747. (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
  748. new->euid = new->uid;
  749. new->egid = new->gid;
  750. }
  751. new->cap_permitted = cap_intersect(new->cap_permitted,
  752. old->cap_permitted);
  753. }
  754. new->suid = new->fsuid = new->euid;
  755. new->sgid = new->fsgid = new->egid;
  756. /* File caps or setid cancels ambient. */
  757. if (has_fcap || is_setid)
  758. cap_clear(new->cap_ambient);
  759. /*
  760. * Now that we've computed pA', update pP' to give:
  761. * pP' = (X & fP) | (pI & fI) | pA'
  762. */
  763. new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
  764. /*
  765. * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
  766. * this is the same as pE' = (fE ? pP' : 0) | pA'.
  767. */
  768. if (effective)
  769. new->cap_effective = new->cap_permitted;
  770. else
  771. new->cap_effective = new->cap_ambient;
  772. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  773. return -EPERM;
  774. if (nonroot_raised_pE(new, old, root_uid, has_fcap)) {
  775. ret = audit_log_bprm_fcaps(bprm, new, old);
  776. if (ret < 0)
  777. return ret;
  778. }
  779. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  780. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  781. return -EPERM;
  782. /* Check for privilege-elevated exec. */
  783. bprm->cap_elevated = 0;
  784. if (is_setid ||
  785. (!__is_real(root_uid, new) &&
  786. (effective ||
  787. __cap_grew(permitted, ambient, new))))
  788. bprm->cap_elevated = 1;
  789. return 0;
  790. }
  791. /**
  792. * cap_inode_setxattr - Determine whether an xattr may be altered
  793. * @dentry: The inode/dentry being altered
  794. * @name: The name of the xattr to be changed
  795. * @value: The value that the xattr will be changed to
  796. * @size: The size of value
  797. * @flags: The replacement flag
  798. *
  799. * Determine whether an xattr may be altered or set on an inode, returning 0 if
  800. * permission is granted, -ve if denied.
  801. *
  802. * This is used to make sure security xattrs don't get updated or set by those
  803. * who aren't privileged to do so.
  804. */
  805. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  806. const void *value, size_t size, int flags)
  807. {
  808. struct user_namespace *user_ns = dentry->d_sb->s_user_ns;
  809. /* Ignore non-security xattrs */
  810. if (strncmp(name, XATTR_SECURITY_PREFIX,
  811. sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
  812. return 0;
  813. /*
  814. * For XATTR_NAME_CAPS the check will be done in
  815. * cap_convert_nscap(), called by setxattr()
  816. */
  817. if (strcmp(name, XATTR_NAME_CAPS) == 0)
  818. return 0;
  819. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  820. return -EPERM;
  821. return 0;
  822. }
  823. /**
  824. * cap_inode_removexattr - Determine whether an xattr may be removed
  825. * @dentry: The inode/dentry being altered
  826. * @name: The name of the xattr to be changed
  827. *
  828. * Determine whether an xattr may be removed from an inode, returning 0 if
  829. * permission is granted, -ve if denied.
  830. *
  831. * This is used to make sure security xattrs don't get removed by those who
  832. * aren't privileged to remove them.
  833. */
  834. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  835. {
  836. struct user_namespace *user_ns = dentry->d_sb->s_user_ns;
  837. /* Ignore non-security xattrs */
  838. if (strncmp(name, XATTR_SECURITY_PREFIX,
  839. sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
  840. return 0;
  841. if (strcmp(name, XATTR_NAME_CAPS) == 0) {
  842. /* security.capability gets namespaced */
  843. struct inode *inode = d_backing_inode(dentry);
  844. if (!inode)
  845. return -EINVAL;
  846. if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
  847. return -EPERM;
  848. return 0;
  849. }
  850. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  851. return -EPERM;
  852. return 0;
  853. }
  854. /*
  855. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  856. * a process after a call to setuid, setreuid, or setresuid.
  857. *
  858. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  859. * {r,e,s}uid != 0, the permitted and effective capabilities are
  860. * cleared.
  861. *
  862. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  863. * capabilities of the process are cleared.
  864. *
  865. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  866. * capabilities are set to the permitted capabilities.
  867. *
  868. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  869. * never happen.
  870. *
  871. * -astor
  872. *
  873. * cevans - New behaviour, Oct '99
  874. * A process may, via prctl(), elect to keep its capabilities when it
  875. * calls setuid() and switches away from uid==0. Both permitted and
  876. * effective sets will be retained.
  877. * Without this change, it was impossible for a daemon to drop only some
  878. * of its privilege. The call to setuid(!=0) would drop all privileges!
  879. * Keeping uid 0 is not an option because uid 0 owns too many vital
  880. * files..
  881. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  882. */
  883. static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
  884. {
  885. kuid_t root_uid = make_kuid(old->user_ns, 0);
  886. if ((uid_eq(old->uid, root_uid) ||
  887. uid_eq(old->euid, root_uid) ||
  888. uid_eq(old->suid, root_uid)) &&
  889. (!uid_eq(new->uid, root_uid) &&
  890. !uid_eq(new->euid, root_uid) &&
  891. !uid_eq(new->suid, root_uid))) {
  892. if (!issecure(SECURE_KEEP_CAPS)) {
  893. cap_clear(new->cap_permitted);
  894. cap_clear(new->cap_effective);
  895. }
  896. /*
  897. * Pre-ambient programs expect setresuid to nonroot followed
  898. * by exec to drop capabilities. We should make sure that
  899. * this remains the case.
  900. */
  901. cap_clear(new->cap_ambient);
  902. }
  903. if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
  904. cap_clear(new->cap_effective);
  905. if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
  906. new->cap_effective = new->cap_permitted;
  907. }
  908. /**
  909. * cap_task_fix_setuid - Fix up the results of setuid() call
  910. * @new: The proposed credentials
  911. * @old: The current task's current credentials
  912. * @flags: Indications of what has changed
  913. *
  914. * Fix up the results of setuid() call before the credential changes are
  915. * actually applied, returning 0 to grant the changes, -ve to deny them.
  916. */
  917. int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
  918. {
  919. switch (flags) {
  920. case LSM_SETID_RE:
  921. case LSM_SETID_ID:
  922. case LSM_SETID_RES:
  923. /* juggle the capabilities to follow [RES]UID changes unless
  924. * otherwise suppressed */
  925. if (!issecure(SECURE_NO_SETUID_FIXUP))
  926. cap_emulate_setxuid(new, old);
  927. break;
  928. case LSM_SETID_FS:
  929. /* juggle the capabilties to follow FSUID changes, unless
  930. * otherwise suppressed
  931. *
  932. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  933. * if not, we might be a bit too harsh here.
  934. */
  935. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  936. kuid_t root_uid = make_kuid(old->user_ns, 0);
  937. if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
  938. new->cap_effective =
  939. cap_drop_fs_set(new->cap_effective);
  940. if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
  941. new->cap_effective =
  942. cap_raise_fs_set(new->cap_effective,
  943. new->cap_permitted);
  944. }
  945. break;
  946. default:
  947. return -EINVAL;
  948. }
  949. return 0;
  950. }
  951. /*
  952. * Rationale: code calling task_setscheduler, task_setioprio, and
  953. * task_setnice, assumes that
  954. * . if capable(cap_sys_nice), then those actions should be allowed
  955. * . if not capable(cap_sys_nice), but acting on your own processes,
  956. * then those actions should be allowed
  957. * This is insufficient now since you can call code without suid, but
  958. * yet with increased caps.
  959. * So we check for increased caps on the target process.
  960. */
  961. static int cap_safe_nice(struct task_struct *p)
  962. {
  963. int is_subset, ret = 0;
  964. rcu_read_lock();
  965. is_subset = cap_issubset(__task_cred(p)->cap_permitted,
  966. current_cred()->cap_permitted);
  967. if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
  968. ret = -EPERM;
  969. rcu_read_unlock();
  970. return ret;
  971. }
  972. /**
  973. * cap_task_setscheduler - Detemine if scheduler policy change is permitted
  974. * @p: The task to affect
  975. *
  976. * Detemine if the requested scheduler policy change is permitted for the
  977. * specified task, returning 0 if permission is granted, -ve if denied.
  978. */
  979. int cap_task_setscheduler(struct task_struct *p)
  980. {
  981. return cap_safe_nice(p);
  982. }
  983. /**
  984. * cap_task_ioprio - Detemine if I/O priority change is permitted
  985. * @p: The task to affect
  986. * @ioprio: The I/O priority to set
  987. *
  988. * Detemine if the requested I/O priority change is permitted for the specified
  989. * task, returning 0 if permission is granted, -ve if denied.
  990. */
  991. int cap_task_setioprio(struct task_struct *p, int ioprio)
  992. {
  993. return cap_safe_nice(p);
  994. }
  995. /**
  996. * cap_task_ioprio - Detemine if task priority change is permitted
  997. * @p: The task to affect
  998. * @nice: The nice value to set
  999. *
  1000. * Detemine if the requested task priority change is permitted for the
  1001. * specified task, returning 0 if permission is granted, -ve if denied.
  1002. */
  1003. int cap_task_setnice(struct task_struct *p, int nice)
  1004. {
  1005. return cap_safe_nice(p);
  1006. }
  1007. /*
  1008. * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
  1009. * the current task's bounding set. Returns 0 on success, -ve on error.
  1010. */
  1011. static int cap_prctl_drop(unsigned long cap)
  1012. {
  1013. struct cred *new;
  1014. if (!ns_capable(current_user_ns(), CAP_SETPCAP))
  1015. return -EPERM;
  1016. if (!cap_valid(cap))
  1017. return -EINVAL;
  1018. new = prepare_creds();
  1019. if (!new)
  1020. return -ENOMEM;
  1021. cap_lower(new->cap_bset, cap);
  1022. return commit_creds(new);
  1023. }
  1024. /**
  1025. * cap_task_prctl - Implement process control functions for this security module
  1026. * @option: The process control function requested
  1027. * @arg2, @arg3, @arg4, @arg5: The argument data for this function
  1028. *
  1029. * Allow process control functions (sys_prctl()) to alter capabilities; may
  1030. * also deny access to other functions not otherwise implemented here.
  1031. *
  1032. * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
  1033. * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
  1034. * modules will consider performing the function.
  1035. */
  1036. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  1037. unsigned long arg4, unsigned long arg5)
  1038. {
  1039. const struct cred *old = current_cred();
  1040. struct cred *new;
  1041. switch (option) {
  1042. case PR_CAPBSET_READ:
  1043. if (!cap_valid(arg2))
  1044. return -EINVAL;
  1045. return !!cap_raised(old->cap_bset, arg2);
  1046. case PR_CAPBSET_DROP:
  1047. return cap_prctl_drop(arg2);
  1048. /*
  1049. * The next four prctl's remain to assist with transitioning a
  1050. * system from legacy UID=0 based privilege (when filesystem
  1051. * capabilities are not in use) to a system using filesystem
  1052. * capabilities only - as the POSIX.1e draft intended.
  1053. *
  1054. * Note:
  1055. *
  1056. * PR_SET_SECUREBITS =
  1057. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  1058. * | issecure_mask(SECURE_NOROOT)
  1059. * | issecure_mask(SECURE_NOROOT_LOCKED)
  1060. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  1061. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  1062. *
  1063. * will ensure that the current process and all of its
  1064. * children will be locked into a pure
  1065. * capability-based-privilege environment.
  1066. */
  1067. case PR_SET_SECUREBITS:
  1068. if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
  1069. & (old->securebits ^ arg2)) /*[1]*/
  1070. || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
  1071. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  1072. || (cap_capable(current_cred(),
  1073. current_cred()->user_ns,
  1074. CAP_SETPCAP,
  1075. CAP_OPT_NONE) != 0) /*[4]*/
  1076. /*
  1077. * [1] no changing of bits that are locked
  1078. * [2] no unlocking of locks
  1079. * [3] no setting of unsupported bits
  1080. * [4] doing anything requires privilege (go read about
  1081. * the "sendmail capabilities bug")
  1082. */
  1083. )
  1084. /* cannot change a locked bit */
  1085. return -EPERM;
  1086. new = prepare_creds();
  1087. if (!new)
  1088. return -ENOMEM;
  1089. new->securebits = arg2;
  1090. return commit_creds(new);
  1091. case PR_GET_SECUREBITS:
  1092. return old->securebits;
  1093. case PR_GET_KEEPCAPS:
  1094. return !!issecure(SECURE_KEEP_CAPS);
  1095. case PR_SET_KEEPCAPS:
  1096. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  1097. return -EINVAL;
  1098. if (issecure(SECURE_KEEP_CAPS_LOCKED))
  1099. return -EPERM;
  1100. new = prepare_creds();
  1101. if (!new)
  1102. return -ENOMEM;
  1103. if (arg2)
  1104. new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  1105. else
  1106. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  1107. return commit_creds(new);
  1108. case PR_CAP_AMBIENT:
  1109. if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
  1110. if (arg3 | arg4 | arg5)
  1111. return -EINVAL;
  1112. new = prepare_creds();
  1113. if (!new)
  1114. return -ENOMEM;
  1115. cap_clear(new->cap_ambient);
  1116. return commit_creds(new);
  1117. }
  1118. if (((!cap_valid(arg3)) | arg4 | arg5))
  1119. return -EINVAL;
  1120. if (arg2 == PR_CAP_AMBIENT_IS_SET) {
  1121. return !!cap_raised(current_cred()->cap_ambient, arg3);
  1122. } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
  1123. arg2 != PR_CAP_AMBIENT_LOWER) {
  1124. return -EINVAL;
  1125. } else {
  1126. if (arg2 == PR_CAP_AMBIENT_RAISE &&
  1127. (!cap_raised(current_cred()->cap_permitted, arg3) ||
  1128. !cap_raised(current_cred()->cap_inheritable,
  1129. arg3) ||
  1130. issecure(SECURE_NO_CAP_AMBIENT_RAISE)))
  1131. return -EPERM;
  1132. new = prepare_creds();
  1133. if (!new)
  1134. return -ENOMEM;
  1135. if (arg2 == PR_CAP_AMBIENT_RAISE)
  1136. cap_raise(new->cap_ambient, arg3);
  1137. else
  1138. cap_lower(new->cap_ambient, arg3);
  1139. return commit_creds(new);
  1140. }
  1141. default:
  1142. /* No functionality available - continue with default */
  1143. return -ENOSYS;
  1144. }
  1145. }
  1146. /**
  1147. * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
  1148. * @mm: The VM space in which the new mapping is to be made
  1149. * @pages: The size of the mapping
  1150. *
  1151. * Determine whether the allocation of a new virtual mapping by the current
  1152. * task is permitted, returning 1 if permission is granted, 0 if not.
  1153. */
  1154. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  1155. {
  1156. int cap_sys_admin = 0;
  1157. if (cap_capable(current_cred(), &init_user_ns,
  1158. CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) == 0)
  1159. cap_sys_admin = 1;
  1160. return cap_sys_admin;
  1161. }
  1162. /*
  1163. * cap_mmap_addr - check if able to map given addr
  1164. * @addr: address attempting to be mapped
  1165. *
  1166. * If the process is attempting to map memory below dac_mmap_min_addr they need
  1167. * CAP_SYS_RAWIO. The other parameters to this function are unused by the
  1168. * capability security module. Returns 0 if this mapping should be allowed
  1169. * -EPERM if not.
  1170. */
  1171. int cap_mmap_addr(unsigned long addr)
  1172. {
  1173. int ret = 0;
  1174. if (addr < dac_mmap_min_addr) {
  1175. ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
  1176. CAP_OPT_NONE);
  1177. /* set PF_SUPERPRIV if it turns out we allow the low mmap */
  1178. if (ret == 0)
  1179. current->flags |= PF_SUPERPRIV;
  1180. }
  1181. return ret;
  1182. }
  1183. int cap_mmap_file(struct file *file, unsigned long reqprot,
  1184. unsigned long prot, unsigned long flags)
  1185. {
  1186. return 0;
  1187. }
  1188. #ifdef CONFIG_SECURITY
  1189. struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
  1190. LSM_HOOK_INIT(capable, cap_capable),
  1191. LSM_HOOK_INIT(settime, cap_settime),
  1192. LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
  1193. LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
  1194. LSM_HOOK_INIT(capget, cap_capget),
  1195. LSM_HOOK_INIT(capset, cap_capset),
  1196. LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
  1197. LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
  1198. LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
  1199. LSM_HOOK_INIT(inode_getsecurity, cap_inode_getsecurity),
  1200. LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
  1201. LSM_HOOK_INIT(mmap_file, cap_mmap_file),
  1202. LSM_HOOK_INIT(task_fix_setuid, cap_task_fix_setuid),
  1203. LSM_HOOK_INIT(task_prctl, cap_task_prctl),
  1204. LSM_HOOK_INIT(task_setscheduler, cap_task_setscheduler),
  1205. LSM_HOOK_INIT(task_setioprio, cap_task_setioprio),
  1206. LSM_HOOK_INIT(task_setnice, cap_task_setnice),
  1207. LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
  1208. };
  1209. void __init capability_add_hooks(void)
  1210. {
  1211. security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks),
  1212. "capability");
  1213. }
  1214. #endif /* CONFIG_SECURITY */