commoncap.c 26 KB

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