commoncap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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/security.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/syslog.h>
  30. /*
  31. * If a non-root user executes a setuid-root binary in
  32. * !secure(SECURE_NOROOT) mode, then we raise capabilities.
  33. * However if fE is also set, then the intent is for only
  34. * the file capabilities to be applied, and the setuid-root
  35. * bit is left on either to change the uid (plausible) or
  36. * to get full privilege on a kernel without file capabilities
  37. * support. So in that case we do not raise capabilities.
  38. *
  39. * Warn if that happens, once per boot.
  40. */
  41. static void warn_setuid_and_fcaps_mixed(const char *fname)
  42. {
  43. static int warned;
  44. if (!warned) {
  45. printk(KERN_INFO "warning: `%s' has both setuid-root and"
  46. " effective capabilities. Therefore not raising all"
  47. " capabilities.\n", fname);
  48. warned = 1;
  49. }
  50. }
  51. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  52. {
  53. NETLINK_CB(skb).eff_cap = current_cap();
  54. return 0;
  55. }
  56. int cap_netlink_recv(struct sk_buff *skb, int cap)
  57. {
  58. if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  59. return -EPERM;
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(cap_netlink_recv);
  63. /**
  64. * cap_capable - Determine whether a task has a particular effective capability
  65. * @tsk: The task to query
  66. * @cred: The credentials to use
  67. * @cap: The capability to check for
  68. * @audit: Whether to write an audit message or not
  69. *
  70. * Determine whether the nominated task has the specified capability amongst
  71. * its effective set, returning 0 if it does, -ve if it does not.
  72. *
  73. * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
  74. * and has_capability() functions. That is, it has the reverse semantics:
  75. * cap_has_capability() returns 0 when a task has a capability, but the
  76. * kernel's capable() and has_capability() returns 1 for this case.
  77. */
  78. int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
  79. int audit)
  80. {
  81. return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
  82. }
  83. /**
  84. * cap_settime - Determine whether the current process may set the system clock
  85. * @ts: The time to set
  86. * @tz: The timezone to set
  87. *
  88. * Determine whether the current process may set the system clock and timezone
  89. * information, returning 0 if permission granted, -ve if denied.
  90. */
  91. int cap_settime(struct timespec *ts, struct timezone *tz)
  92. {
  93. if (!capable(CAP_SYS_TIME))
  94. return -EPERM;
  95. return 0;
  96. }
  97. /**
  98. * cap_ptrace_access_check - Determine whether the current process may access
  99. * another
  100. * @child: The process to be accessed
  101. * @mode: The mode of attachment.
  102. *
  103. * Determine whether a process may access another, returning 0 if permission
  104. * granted, -ve if denied.
  105. */
  106. int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
  107. {
  108. int ret = 0;
  109. rcu_read_lock();
  110. if (!cap_issubset(__task_cred(child)->cap_permitted,
  111. current_cred()->cap_permitted) &&
  112. !capable(CAP_SYS_PTRACE))
  113. ret = -EPERM;
  114. rcu_read_unlock();
  115. return ret;
  116. }
  117. /**
  118. * cap_ptrace_traceme - Determine whether another process may trace the current
  119. * @parent: The task proposed to be the tracer
  120. *
  121. * Determine whether the nominated task is permitted to trace the current
  122. * process, returning 0 if permission is granted, -ve if denied.
  123. */
  124. int cap_ptrace_traceme(struct task_struct *parent)
  125. {
  126. int ret = 0;
  127. rcu_read_lock();
  128. if (!cap_issubset(current_cred()->cap_permitted,
  129. __task_cred(parent)->cap_permitted) &&
  130. !has_capability(parent, CAP_SYS_PTRACE))
  131. ret = -EPERM;
  132. rcu_read_unlock();
  133. return ret;
  134. }
  135. /**
  136. * cap_capget - Retrieve a task's capability sets
  137. * @target: The task from which to retrieve the capability sets
  138. * @effective: The place to record the effective set
  139. * @inheritable: The place to record the inheritable set
  140. * @permitted: The place to record the permitted set
  141. *
  142. * This function retrieves the capabilities of the nominated task and returns
  143. * them to the caller.
  144. */
  145. int cap_capget(struct task_struct *target, kernel_cap_t *effective,
  146. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  147. {
  148. const struct cred *cred;
  149. /* Derived from kernel/capability.c:sys_capget. */
  150. rcu_read_lock();
  151. cred = __task_cred(target);
  152. *effective = cred->cap_effective;
  153. *inheritable = cred->cap_inheritable;
  154. *permitted = cred->cap_permitted;
  155. rcu_read_unlock();
  156. return 0;
  157. }
  158. /*
  159. * Determine whether the inheritable capabilities are limited to the old
  160. * permitted set. Returns 1 if they are limited, 0 if they are not.
  161. */
  162. static inline int cap_inh_is_capped(void)
  163. {
  164. /* they are so limited unless the current task has the CAP_SETPCAP
  165. * capability
  166. */
  167. if (cap_capable(current, current_cred(), CAP_SETPCAP,
  168. SECURITY_CAP_AUDIT) == 0)
  169. return 0;
  170. return 1;
  171. }
  172. /**
  173. * cap_capset - Validate and apply proposed changes to current's capabilities
  174. * @new: The proposed new credentials; alterations should be made here
  175. * @old: The current task's current credentials
  176. * @effective: A pointer to the proposed new effective capabilities set
  177. * @inheritable: A pointer to the proposed new inheritable capabilities set
  178. * @permitted: A pointer to the proposed new permitted capabilities set
  179. *
  180. * This function validates and applies a proposed mass change to the current
  181. * process's capability sets. The changes are made to the proposed new
  182. * credentials, and assuming no error, will be committed by the caller of LSM.
  183. */
  184. int cap_capset(struct cred *new,
  185. const struct cred *old,
  186. const kernel_cap_t *effective,
  187. const kernel_cap_t *inheritable,
  188. const kernel_cap_t *permitted)
  189. {
  190. if (cap_inh_is_capped() &&
  191. !cap_issubset(*inheritable,
  192. cap_combine(old->cap_inheritable,
  193. old->cap_permitted)))
  194. /* incapable of using this inheritable set */
  195. return -EPERM;
  196. if (!cap_issubset(*inheritable,
  197. cap_combine(old->cap_inheritable,
  198. old->cap_bset)))
  199. /* no new pI capabilities outside bounding set */
  200. return -EPERM;
  201. /* verify restrictions on target's new Permitted set */
  202. if (!cap_issubset(*permitted, old->cap_permitted))
  203. return -EPERM;
  204. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  205. if (!cap_issubset(*effective, *permitted))
  206. return -EPERM;
  207. new->cap_effective = *effective;
  208. new->cap_inheritable = *inheritable;
  209. new->cap_permitted = *permitted;
  210. return 0;
  211. }
  212. /*
  213. * Clear proposed capability sets for execve().
  214. */
  215. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  216. {
  217. cap_clear(bprm->cred->cap_permitted);
  218. bprm->cap_effective = false;
  219. }
  220. /**
  221. * cap_inode_need_killpriv - Determine if inode change affects privileges
  222. * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
  223. *
  224. * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
  225. * affects the security markings on that inode, and if it is, should
  226. * inode_killpriv() be invoked or the change rejected?
  227. *
  228. * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
  229. * -ve to deny the change.
  230. */
  231. int cap_inode_need_killpriv(struct dentry *dentry)
  232. {
  233. struct inode *inode = dentry->d_inode;
  234. int error;
  235. if (!inode->i_op->getxattr)
  236. return 0;
  237. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  238. if (error <= 0)
  239. return 0;
  240. return 1;
  241. }
  242. /**
  243. * cap_inode_killpriv - Erase the security markings on an inode
  244. * @dentry: The inode/dentry to alter
  245. *
  246. * Erase the privilege-enhancing security markings on an inode.
  247. *
  248. * Returns 0 if successful, -ve on error.
  249. */
  250. int cap_inode_killpriv(struct dentry *dentry)
  251. {
  252. struct inode *inode = dentry->d_inode;
  253. if (!inode->i_op->removexattr)
  254. return 0;
  255. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  256. }
  257. /*
  258. * Calculate the new process capability sets from the capability sets attached
  259. * to a file.
  260. */
  261. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  262. struct linux_binprm *bprm,
  263. bool *effective)
  264. {
  265. struct cred *new = bprm->cred;
  266. unsigned i;
  267. int ret = 0;
  268. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  269. *effective = true;
  270. CAP_FOR_EACH_U32(i) {
  271. __u32 permitted = caps->permitted.cap[i];
  272. __u32 inheritable = caps->inheritable.cap[i];
  273. /*
  274. * pP' = (X & fP) | (pI & fI)
  275. */
  276. new->cap_permitted.cap[i] =
  277. (new->cap_bset.cap[i] & permitted) |
  278. (new->cap_inheritable.cap[i] & inheritable);
  279. if (permitted & ~new->cap_permitted.cap[i])
  280. /* insufficient to execute correctly */
  281. ret = -EPERM;
  282. }
  283. /*
  284. * For legacy apps, with no internal support for recognizing they
  285. * do not have enough capabilities, we return an error if they are
  286. * missing some "forced" (aka file-permitted) capabilities.
  287. */
  288. return *effective ? ret : 0;
  289. }
  290. /*
  291. * Extract the on-exec-apply capability sets for an executable file.
  292. */
  293. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  294. {
  295. struct inode *inode = dentry->d_inode;
  296. __u32 magic_etc;
  297. unsigned tocopy, i;
  298. int size;
  299. struct vfs_cap_data caps;
  300. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  301. if (!inode || !inode->i_op->getxattr)
  302. return -ENODATA;
  303. size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
  304. XATTR_CAPS_SZ);
  305. if (size == -ENODATA || size == -EOPNOTSUPP)
  306. /* no data, that's ok */
  307. return -ENODATA;
  308. if (size < 0)
  309. return size;
  310. if (size < sizeof(magic_etc))
  311. return -EINVAL;
  312. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
  313. switch (magic_etc & VFS_CAP_REVISION_MASK) {
  314. case VFS_CAP_REVISION_1:
  315. if (size != XATTR_CAPS_SZ_1)
  316. return -EINVAL;
  317. tocopy = VFS_CAP_U32_1;
  318. break;
  319. case VFS_CAP_REVISION_2:
  320. if (size != XATTR_CAPS_SZ_2)
  321. return -EINVAL;
  322. tocopy = VFS_CAP_U32_2;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. CAP_FOR_EACH_U32(i) {
  328. if (i >= tocopy)
  329. break;
  330. cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
  331. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
  332. }
  333. return 0;
  334. }
  335. /*
  336. * Attempt to get the on-exec apply capability sets for an executable file from
  337. * its xattrs and, if present, apply them to the proposed credentials being
  338. * constructed by execve().
  339. */
  340. static int get_file_caps(struct linux_binprm *bprm, bool *effective)
  341. {
  342. struct dentry *dentry;
  343. int rc = 0;
  344. struct cpu_vfs_cap_data vcaps;
  345. bprm_clear_caps(bprm);
  346. if (!file_caps_enabled)
  347. return 0;
  348. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
  349. return 0;
  350. dentry = dget(bprm->file->f_dentry);
  351. rc = get_vfs_caps_from_disk(dentry, &vcaps);
  352. if (rc < 0) {
  353. if (rc == -EINVAL)
  354. printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
  355. __func__, rc, bprm->filename);
  356. else if (rc == -ENODATA)
  357. rc = 0;
  358. goto out;
  359. }
  360. rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
  361. if (rc == -EINVAL)
  362. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  363. __func__, rc, bprm->filename);
  364. out:
  365. dput(dentry);
  366. if (rc)
  367. bprm_clear_caps(bprm);
  368. return rc;
  369. }
  370. /**
  371. * cap_bprm_set_creds - Set up the proposed credentials for execve().
  372. * @bprm: The execution parameters, including the proposed creds
  373. *
  374. * Set up the proposed credentials for a new execution context being
  375. * constructed by execve(). The proposed creds in @bprm->cred is altered,
  376. * which won't take effect immediately. Returns 0 if successful, -ve on error.
  377. */
  378. int cap_bprm_set_creds(struct linux_binprm *bprm)
  379. {
  380. const struct cred *old = current_cred();
  381. struct cred *new = bprm->cred;
  382. bool effective;
  383. int ret;
  384. effective = false;
  385. ret = get_file_caps(bprm, &effective);
  386. if (ret < 0)
  387. return ret;
  388. if (!issecure(SECURE_NOROOT)) {
  389. /*
  390. * If the legacy file capability is set, then don't set privs
  391. * for a setuid root binary run by a non-root user. Do set it
  392. * for a root user just to cause least surprise to an admin.
  393. */
  394. if (effective && new->uid != 0 && new->euid == 0) {
  395. warn_setuid_and_fcaps_mixed(bprm->filename);
  396. goto skip;
  397. }
  398. /*
  399. * To support inheritance of root-permissions and suid-root
  400. * executables under compatibility mode, we override the
  401. * capability sets for the file.
  402. *
  403. * If only the real uid is 0, we do not set the effective bit.
  404. */
  405. if (new->euid == 0 || new->uid == 0) {
  406. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  407. new->cap_permitted = cap_combine(old->cap_bset,
  408. old->cap_inheritable);
  409. }
  410. if (new->euid == 0)
  411. effective = true;
  412. }
  413. skip:
  414. /* Don't let someone trace a set[ug]id/setpcap binary with the revised
  415. * credentials unless they have the appropriate permit
  416. */
  417. if ((new->euid != old->uid ||
  418. new->egid != old->gid ||
  419. !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
  420. bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  421. /* downgrade; they get no more than they had, and maybe less */
  422. if (!capable(CAP_SETUID)) {
  423. new->euid = new->uid;
  424. new->egid = new->gid;
  425. }
  426. new->cap_permitted = cap_intersect(new->cap_permitted,
  427. old->cap_permitted);
  428. }
  429. new->suid = new->fsuid = new->euid;
  430. new->sgid = new->fsgid = new->egid;
  431. /* For init, we want to retain the capabilities set in the initial
  432. * task. Thus we skip the usual capability rules
  433. */
  434. if (!is_global_init(current)) {
  435. if (effective)
  436. new->cap_effective = new->cap_permitted;
  437. else
  438. cap_clear(new->cap_effective);
  439. }
  440. bprm->cap_effective = effective;
  441. /*
  442. * Audit candidate if current->cap_effective is set
  443. *
  444. * We do not bother to audit if 3 things are true:
  445. * 1) cap_effective has all caps
  446. * 2) we are root
  447. * 3) root is supposed to have all caps (SECURE_NOROOT)
  448. * Since this is just a normal root execing a process.
  449. *
  450. * Number 1 above might fail if you don't have a full bset, but I think
  451. * that is interesting information to audit.
  452. */
  453. if (!cap_isclear(new->cap_effective)) {
  454. if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
  455. new->euid != 0 || new->uid != 0 ||
  456. issecure(SECURE_NOROOT)) {
  457. ret = audit_log_bprm_fcaps(bprm, new, old);
  458. if (ret < 0)
  459. return ret;
  460. }
  461. }
  462. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  463. return 0;
  464. }
  465. /**
  466. * cap_bprm_secureexec - Determine whether a secure execution is required
  467. * @bprm: The execution parameters
  468. *
  469. * Determine whether a secure execution is required, return 1 if it is, and 0
  470. * if it is not.
  471. *
  472. * The credentials have been committed by this point, and so are no longer
  473. * available through @bprm->cred.
  474. */
  475. int cap_bprm_secureexec(struct linux_binprm *bprm)
  476. {
  477. const struct cred *cred = current_cred();
  478. if (cred->uid != 0) {
  479. if (bprm->cap_effective)
  480. return 1;
  481. if (!cap_isclear(cred->cap_permitted))
  482. return 1;
  483. }
  484. return (cred->euid != cred->uid ||
  485. cred->egid != cred->gid);
  486. }
  487. /**
  488. * cap_inode_setxattr - Determine whether an xattr may be altered
  489. * @dentry: The inode/dentry being altered
  490. * @name: The name of the xattr to be changed
  491. * @value: The value that the xattr will be changed to
  492. * @size: The size of value
  493. * @flags: The replacement flag
  494. *
  495. * Determine whether an xattr may be altered or set on an inode, returning 0 if
  496. * permission is granted, -ve if denied.
  497. *
  498. * This is used to make sure security xattrs don't get updated or set by those
  499. * who aren't privileged to do so.
  500. */
  501. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  502. const void *value, size_t size, int flags)
  503. {
  504. if (!strcmp(name, XATTR_NAME_CAPS)) {
  505. if (!capable(CAP_SETFCAP))
  506. return -EPERM;
  507. return 0;
  508. }
  509. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  510. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  511. !capable(CAP_SYS_ADMIN))
  512. return -EPERM;
  513. return 0;
  514. }
  515. /**
  516. * cap_inode_removexattr - Determine whether an xattr may be removed
  517. * @dentry: The inode/dentry being altered
  518. * @name: The name of the xattr to be changed
  519. *
  520. * Determine whether an xattr may be removed from an inode, returning 0 if
  521. * permission is granted, -ve if denied.
  522. *
  523. * This is used to make sure security xattrs don't get removed by those who
  524. * aren't privileged to remove them.
  525. */
  526. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  527. {
  528. if (!strcmp(name, XATTR_NAME_CAPS)) {
  529. if (!capable(CAP_SETFCAP))
  530. return -EPERM;
  531. return 0;
  532. }
  533. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  534. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  535. !capable(CAP_SYS_ADMIN))
  536. return -EPERM;
  537. return 0;
  538. }
  539. /*
  540. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  541. * a process after a call to setuid, setreuid, or setresuid.
  542. *
  543. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  544. * {r,e,s}uid != 0, the permitted and effective capabilities are
  545. * cleared.
  546. *
  547. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  548. * capabilities of the process are cleared.
  549. *
  550. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  551. * capabilities are set to the permitted capabilities.
  552. *
  553. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  554. * never happen.
  555. *
  556. * -astor
  557. *
  558. * cevans - New behaviour, Oct '99
  559. * A process may, via prctl(), elect to keep its capabilities when it
  560. * calls setuid() and switches away from uid==0. Both permitted and
  561. * effective sets will be retained.
  562. * Without this change, it was impossible for a daemon to drop only some
  563. * of its privilege. The call to setuid(!=0) would drop all privileges!
  564. * Keeping uid 0 is not an option because uid 0 owns too many vital
  565. * files..
  566. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  567. */
  568. static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
  569. {
  570. if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
  571. (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
  572. !issecure(SECURE_KEEP_CAPS)) {
  573. cap_clear(new->cap_permitted);
  574. cap_clear(new->cap_effective);
  575. }
  576. if (old->euid == 0 && new->euid != 0)
  577. cap_clear(new->cap_effective);
  578. if (old->euid != 0 && new->euid == 0)
  579. new->cap_effective = new->cap_permitted;
  580. }
  581. /**
  582. * cap_task_fix_setuid - Fix up the results of setuid() call
  583. * @new: The proposed credentials
  584. * @old: The current task's current credentials
  585. * @flags: Indications of what has changed
  586. *
  587. * Fix up the results of setuid() call before the credential changes are
  588. * actually applied, returning 0 to grant the changes, -ve to deny them.
  589. */
  590. int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
  591. {
  592. switch (flags) {
  593. case LSM_SETID_RE:
  594. case LSM_SETID_ID:
  595. case LSM_SETID_RES:
  596. /* juggle the capabilities to follow [RES]UID changes unless
  597. * otherwise suppressed */
  598. if (!issecure(SECURE_NO_SETUID_FIXUP))
  599. cap_emulate_setxuid(new, old);
  600. break;
  601. case LSM_SETID_FS:
  602. /* juggle the capabilties to follow FSUID changes, unless
  603. * otherwise suppressed
  604. *
  605. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  606. * if not, we might be a bit too harsh here.
  607. */
  608. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  609. if (old->fsuid == 0 && new->fsuid != 0)
  610. new->cap_effective =
  611. cap_drop_fs_set(new->cap_effective);
  612. if (old->fsuid != 0 && new->fsuid == 0)
  613. new->cap_effective =
  614. cap_raise_fs_set(new->cap_effective,
  615. new->cap_permitted);
  616. }
  617. break;
  618. default:
  619. return -EINVAL;
  620. }
  621. return 0;
  622. }
  623. /*
  624. * Rationale: code calling task_setscheduler, task_setioprio, and
  625. * task_setnice, assumes that
  626. * . if capable(cap_sys_nice), then those actions should be allowed
  627. * . if not capable(cap_sys_nice), but acting on your own processes,
  628. * then those actions should be allowed
  629. * This is insufficient now since you can call code without suid, but
  630. * yet with increased caps.
  631. * So we check for increased caps on the target process.
  632. */
  633. static int cap_safe_nice(struct task_struct *p)
  634. {
  635. int is_subset;
  636. rcu_read_lock();
  637. is_subset = cap_issubset(__task_cred(p)->cap_permitted,
  638. current_cred()->cap_permitted);
  639. rcu_read_unlock();
  640. if (!is_subset && !capable(CAP_SYS_NICE))
  641. return -EPERM;
  642. return 0;
  643. }
  644. /**
  645. * cap_task_setscheduler - Detemine if scheduler policy change is permitted
  646. * @p: The task to affect
  647. * @policy: The policy to effect
  648. * @lp: The parameters to the scheduling policy
  649. *
  650. * Detemine if the requested scheduler policy change is permitted for the
  651. * specified task, returning 0 if permission is granted, -ve if denied.
  652. */
  653. int cap_task_setscheduler(struct task_struct *p, int policy,
  654. struct sched_param *lp)
  655. {
  656. return cap_safe_nice(p);
  657. }
  658. /**
  659. * cap_task_ioprio - Detemine if I/O priority change is permitted
  660. * @p: The task to affect
  661. * @ioprio: The I/O priority to set
  662. *
  663. * Detemine if the requested I/O priority change is permitted for the specified
  664. * task, returning 0 if permission is granted, -ve if denied.
  665. */
  666. int cap_task_setioprio(struct task_struct *p, int ioprio)
  667. {
  668. return cap_safe_nice(p);
  669. }
  670. /**
  671. * cap_task_ioprio - Detemine if task priority change is permitted
  672. * @p: The task to affect
  673. * @nice: The nice value to set
  674. *
  675. * Detemine if the requested task priority change is permitted for the
  676. * specified task, returning 0 if permission is granted, -ve if denied.
  677. */
  678. int cap_task_setnice(struct task_struct *p, int nice)
  679. {
  680. return cap_safe_nice(p);
  681. }
  682. /*
  683. * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
  684. * the current task's bounding set. Returns 0 on success, -ve on error.
  685. */
  686. static long cap_prctl_drop(struct cred *new, unsigned long cap)
  687. {
  688. if (!capable(CAP_SETPCAP))
  689. return -EPERM;
  690. if (!cap_valid(cap))
  691. return -EINVAL;
  692. cap_lower(new->cap_bset, cap);
  693. return 0;
  694. }
  695. /**
  696. * cap_task_prctl - Implement process control functions for this security module
  697. * @option: The process control function requested
  698. * @arg2, @arg3, @arg4, @arg5: The argument data for this function
  699. *
  700. * Allow process control functions (sys_prctl()) to alter capabilities; may
  701. * also deny access to other functions not otherwise implemented here.
  702. *
  703. * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
  704. * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
  705. * modules will consider performing the function.
  706. */
  707. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  708. unsigned long arg4, unsigned long arg5)
  709. {
  710. struct cred *new;
  711. long error = 0;
  712. new = prepare_creds();
  713. if (!new)
  714. return -ENOMEM;
  715. switch (option) {
  716. case PR_CAPBSET_READ:
  717. error = -EINVAL;
  718. if (!cap_valid(arg2))
  719. goto error;
  720. error = !!cap_raised(new->cap_bset, arg2);
  721. goto no_change;
  722. case PR_CAPBSET_DROP:
  723. error = cap_prctl_drop(new, arg2);
  724. if (error < 0)
  725. goto error;
  726. goto changed;
  727. /*
  728. * The next four prctl's remain to assist with transitioning a
  729. * system from legacy UID=0 based privilege (when filesystem
  730. * capabilities are not in use) to a system using filesystem
  731. * capabilities only - as the POSIX.1e draft intended.
  732. *
  733. * Note:
  734. *
  735. * PR_SET_SECUREBITS =
  736. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  737. * | issecure_mask(SECURE_NOROOT)
  738. * | issecure_mask(SECURE_NOROOT_LOCKED)
  739. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  740. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  741. *
  742. * will ensure that the current process and all of its
  743. * children will be locked into a pure
  744. * capability-based-privilege environment.
  745. */
  746. case PR_SET_SECUREBITS:
  747. error = -EPERM;
  748. if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
  749. & (new->securebits ^ arg2)) /*[1]*/
  750. || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
  751. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  752. || (cap_capable(current, current_cred(), CAP_SETPCAP,
  753. SECURITY_CAP_AUDIT) != 0) /*[4]*/
  754. /*
  755. * [1] no changing of bits that are locked
  756. * [2] no unlocking of locks
  757. * [3] no setting of unsupported bits
  758. * [4] doing anything requires privilege (go read about
  759. * the "sendmail capabilities bug")
  760. */
  761. )
  762. /* cannot change a locked bit */
  763. goto error;
  764. new->securebits = arg2;
  765. goto changed;
  766. case PR_GET_SECUREBITS:
  767. error = new->securebits;
  768. goto no_change;
  769. case PR_GET_KEEPCAPS:
  770. if (issecure(SECURE_KEEP_CAPS))
  771. error = 1;
  772. goto no_change;
  773. case PR_SET_KEEPCAPS:
  774. error = -EINVAL;
  775. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  776. goto error;
  777. error = -EPERM;
  778. if (issecure(SECURE_KEEP_CAPS_LOCKED))
  779. goto error;
  780. if (arg2)
  781. new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  782. else
  783. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  784. goto changed;
  785. default:
  786. /* No functionality available - continue with default */
  787. error = -ENOSYS;
  788. goto error;
  789. }
  790. /* Functionality provided */
  791. changed:
  792. return commit_creds(new);
  793. no_change:
  794. error:
  795. abort_creds(new);
  796. return error;
  797. }
  798. /**
  799. * cap_syslog - Determine whether syslog function is permitted
  800. * @type: Function requested
  801. * @from_file: Whether this request came from an open file (i.e. /proc)
  802. *
  803. * Determine whether the current process is permitted to use a particular
  804. * syslog function, returning 0 if permission is granted, -ve if not.
  805. */
  806. int cap_syslog(int type, bool from_file)
  807. {
  808. if (type != SYSLOG_ACTION_OPEN && from_file)
  809. return 0;
  810. if ((type != SYSLOG_ACTION_READ_ALL &&
  811. type != SYSLOG_ACTION_SIZE_BUFFER) && !capable(CAP_SYS_ADMIN))
  812. return -EPERM;
  813. return 0;
  814. }
  815. /**
  816. * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
  817. * @mm: The VM space in which the new mapping is to be made
  818. * @pages: The size of the mapping
  819. *
  820. * Determine whether the allocation of a new virtual mapping by the current
  821. * task is permitted, returning 0 if permission is granted, -ve if not.
  822. */
  823. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  824. {
  825. int cap_sys_admin = 0;
  826. if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
  827. SECURITY_CAP_NOAUDIT) == 0)
  828. cap_sys_admin = 1;
  829. return __vm_enough_memory(mm, pages, cap_sys_admin);
  830. }
  831. /*
  832. * cap_file_mmap - check if able to map given addr
  833. * @file: unused
  834. * @reqprot: unused
  835. * @prot: unused
  836. * @flags: unused
  837. * @addr: address attempting to be mapped
  838. * @addr_only: unused
  839. *
  840. * If the process is attempting to map memory below dac_mmap_min_addr they need
  841. * CAP_SYS_RAWIO. The other parameters to this function are unused by the
  842. * capability security module. Returns 0 if this mapping should be allowed
  843. * -EPERM if not.
  844. */
  845. int cap_file_mmap(struct file *file, unsigned long reqprot,
  846. unsigned long prot, unsigned long flags,
  847. unsigned long addr, unsigned long addr_only)
  848. {
  849. int ret = 0;
  850. if (addr < dac_mmap_min_addr) {
  851. ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO,
  852. SECURITY_CAP_AUDIT);
  853. /* set PF_SUPERPRIV if it turns out we allow the low mmap */
  854. if (ret == 0)
  855. current->flags |= PF_SUPERPRIV;
  856. }
  857. return ret;
  858. }