commoncap.c 43 KB

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