namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Andrei Vagin <avagin@openvz.org>
  4. * Author: Dmitry Safonov <dima@arista.com>
  5. */
  6. #include <linux/time_namespace.h>
  7. #include <linux/user_namespace.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/proc_ns.h>
  13. #include <linux/export.h>
  14. #include <linux/time.h>
  15. #include <linux/slab.h>
  16. #include <linux/cred.h>
  17. #include <linux/err.h>
  18. #include <linux/mm.h>
  19. #include <vdso/datapage.h>
  20. ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
  21. struct timens_offsets *ns_offsets)
  22. {
  23. ktime_t offset;
  24. switch (clockid) {
  25. case CLOCK_MONOTONIC:
  26. offset = timespec64_to_ktime(ns_offsets->monotonic);
  27. break;
  28. case CLOCK_BOOTTIME:
  29. case CLOCK_BOOTTIME_ALARM:
  30. offset = timespec64_to_ktime(ns_offsets->boottime);
  31. break;
  32. default:
  33. return tim;
  34. }
  35. /*
  36. * Check that @tim value is in [offset, KTIME_MAX + offset]
  37. * and subtract offset.
  38. */
  39. if (tim < offset) {
  40. /*
  41. * User can specify @tim *absolute* value - if it's lesser than
  42. * the time namespace's offset - it's already expired.
  43. */
  44. tim = 0;
  45. } else {
  46. tim = ktime_sub(tim, offset);
  47. if (unlikely(tim > KTIME_MAX))
  48. tim = KTIME_MAX;
  49. }
  50. return tim;
  51. }
  52. static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
  53. {
  54. return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);
  55. }
  56. static void dec_time_namespaces(struct ucounts *ucounts)
  57. {
  58. dec_ucount(ucounts, UCOUNT_TIME_NAMESPACES);
  59. }
  60. /**
  61. * clone_time_ns - Clone a time namespace
  62. * @user_ns: User namespace which owns a new namespace.
  63. * @old_ns: Namespace to clone
  64. *
  65. * Clone @old_ns and set the clone refcount to 1
  66. *
  67. * Return: The new namespace or ERR_PTR.
  68. */
  69. static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
  70. struct time_namespace *old_ns)
  71. {
  72. struct time_namespace *ns;
  73. struct ucounts *ucounts;
  74. int err;
  75. err = -ENOSPC;
  76. ucounts = inc_time_namespaces(user_ns);
  77. if (!ucounts)
  78. goto fail;
  79. err = -ENOMEM;
  80. ns = kmalloc(sizeof(*ns), GFP_KERNEL);
  81. if (!ns)
  82. goto fail_dec;
  83. kref_init(&ns->kref);
  84. ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  85. if (!ns->vvar_page)
  86. goto fail_free;
  87. err = ns_alloc_inum(&ns->ns);
  88. if (err)
  89. goto fail_free_page;
  90. ns->ucounts = ucounts;
  91. ns->ns.ops = &timens_operations;
  92. ns->user_ns = get_user_ns(user_ns);
  93. ns->offsets = old_ns->offsets;
  94. ns->frozen_offsets = false;
  95. return ns;
  96. fail_free_page:
  97. __free_page(ns->vvar_page);
  98. fail_free:
  99. kfree(ns);
  100. fail_dec:
  101. dec_time_namespaces(ucounts);
  102. fail:
  103. return ERR_PTR(err);
  104. }
  105. /**
  106. * copy_time_ns - Create timens_for_children from @old_ns
  107. * @flags: Cloning flags
  108. * @user_ns: User namespace which owns a new namespace.
  109. * @old_ns: Namespace to clone
  110. *
  111. * If CLONE_NEWTIME specified in @flags, creates a new timens_for_children;
  112. * adds a refcounter to @old_ns otherwise.
  113. *
  114. * Return: timens_for_children namespace or ERR_PTR.
  115. */
  116. struct time_namespace *copy_time_ns(unsigned long flags,
  117. struct user_namespace *user_ns, struct time_namespace *old_ns)
  118. {
  119. if (!(flags & CLONE_NEWTIME))
  120. return get_time_ns(old_ns);
  121. return clone_time_ns(user_ns, old_ns);
  122. }
  123. static struct timens_offset offset_from_ts(struct timespec64 off)
  124. {
  125. struct timens_offset ret;
  126. ret.sec = off.tv_sec;
  127. ret.nsec = off.tv_nsec;
  128. return ret;
  129. }
  130. /*
  131. * A time namespace VVAR page has the same layout as the VVAR page which
  132. * contains the system wide VDSO data.
  133. *
  134. * For a normal task the VVAR pages are installed in the normal ordering:
  135. * VVAR
  136. * PVCLOCK
  137. * HVCLOCK
  138. * TIMENS <- Not really required
  139. *
  140. * Now for a timens task the pages are installed in the following order:
  141. * TIMENS
  142. * PVCLOCK
  143. * HVCLOCK
  144. * VVAR
  145. *
  146. * The check for vdso_data->clock_mode is in the unlikely path of
  147. * the seq begin magic. So for the non-timens case most of the time
  148. * 'seq' is even, so the branch is not taken.
  149. *
  150. * If 'seq' is odd, i.e. a concurrent update is in progress, the extra check
  151. * for vdso_data->clock_mode is a non-issue. The task is spin waiting for the
  152. * update to finish and for 'seq' to become even anyway.
  153. *
  154. * Timens page has vdso_data->clock_mode set to VDSO_CLOCKMODE_TIMENS which
  155. * enforces the time namespace handling path.
  156. */
  157. static void timens_setup_vdso_data(struct vdso_data *vdata,
  158. struct time_namespace *ns)
  159. {
  160. struct timens_offset *offset = vdata->offset;
  161. struct timens_offset monotonic = offset_from_ts(ns->offsets.monotonic);
  162. struct timens_offset boottime = offset_from_ts(ns->offsets.boottime);
  163. vdata->seq = 1;
  164. vdata->clock_mode = VDSO_CLOCKMODE_TIMENS;
  165. offset[CLOCK_MONOTONIC] = monotonic;
  166. offset[CLOCK_MONOTONIC_RAW] = monotonic;
  167. offset[CLOCK_MONOTONIC_COARSE] = monotonic;
  168. offset[CLOCK_BOOTTIME] = boottime;
  169. offset[CLOCK_BOOTTIME_ALARM] = boottime;
  170. }
  171. /*
  172. * Protects possibly multiple offsets writers racing each other
  173. * and tasks entering the namespace.
  174. */
  175. static DEFINE_MUTEX(offset_lock);
  176. static void timens_set_vvar_page(struct task_struct *task,
  177. struct time_namespace *ns)
  178. {
  179. struct vdso_data *vdata;
  180. unsigned int i;
  181. if (ns == &init_time_ns)
  182. return;
  183. /* Fast-path, taken by every task in namespace except the first. */
  184. if (likely(ns->frozen_offsets))
  185. return;
  186. mutex_lock(&offset_lock);
  187. /* Nothing to-do: vvar_page has been already initialized. */
  188. if (ns->frozen_offsets)
  189. goto out;
  190. ns->frozen_offsets = true;
  191. vdata = arch_get_vdso_data(page_address(ns->vvar_page));
  192. for (i = 0; i < CS_BASES; i++)
  193. timens_setup_vdso_data(&vdata[i], ns);
  194. out:
  195. mutex_unlock(&offset_lock);
  196. }
  197. void free_time_ns(struct kref *kref)
  198. {
  199. struct time_namespace *ns;
  200. ns = container_of(kref, struct time_namespace, kref);
  201. dec_time_namespaces(ns->ucounts);
  202. put_user_ns(ns->user_ns);
  203. ns_free_inum(&ns->ns);
  204. __free_page(ns->vvar_page);
  205. kfree(ns);
  206. }
  207. static struct time_namespace *to_time_ns(struct ns_common *ns)
  208. {
  209. return container_of(ns, struct time_namespace, ns);
  210. }
  211. static struct ns_common *timens_get(struct task_struct *task)
  212. {
  213. struct time_namespace *ns = NULL;
  214. struct nsproxy *nsproxy;
  215. task_lock(task);
  216. nsproxy = task->nsproxy;
  217. if (nsproxy) {
  218. ns = nsproxy->time_ns;
  219. get_time_ns(ns);
  220. }
  221. task_unlock(task);
  222. return ns ? &ns->ns : NULL;
  223. }
  224. static struct ns_common *timens_for_children_get(struct task_struct *task)
  225. {
  226. struct time_namespace *ns = NULL;
  227. struct nsproxy *nsproxy;
  228. task_lock(task);
  229. nsproxy = task->nsproxy;
  230. if (nsproxy) {
  231. ns = nsproxy->time_ns_for_children;
  232. get_time_ns(ns);
  233. }
  234. task_unlock(task);
  235. return ns ? &ns->ns : NULL;
  236. }
  237. static void timens_put(struct ns_common *ns)
  238. {
  239. put_time_ns(to_time_ns(ns));
  240. }
  241. void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
  242. {
  243. timens_set_vvar_page(tsk, ns);
  244. vdso_join_timens(tsk, ns);
  245. }
  246. static int timens_install(struct nsset *nsset, struct ns_common *new)
  247. {
  248. struct nsproxy *nsproxy = nsset->nsproxy;
  249. struct time_namespace *ns = to_time_ns(new);
  250. if (!current_is_single_threaded())
  251. return -EUSERS;
  252. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  253. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  254. return -EPERM;
  255. get_time_ns(ns);
  256. put_time_ns(nsproxy->time_ns);
  257. nsproxy->time_ns = ns;
  258. get_time_ns(ns);
  259. put_time_ns(nsproxy->time_ns_for_children);
  260. nsproxy->time_ns_for_children = ns;
  261. return 0;
  262. }
  263. int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
  264. {
  265. struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
  266. struct time_namespace *ns = to_time_ns(nsc);
  267. /* create_new_namespaces() already incremented the ref counter */
  268. if (nsproxy->time_ns == nsproxy->time_ns_for_children)
  269. return 0;
  270. get_time_ns(ns);
  271. put_time_ns(nsproxy->time_ns);
  272. nsproxy->time_ns = ns;
  273. timens_commit(tsk, ns);
  274. return 0;
  275. }
  276. static struct user_namespace *timens_owner(struct ns_common *ns)
  277. {
  278. return to_time_ns(ns)->user_ns;
  279. }
  280. static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
  281. {
  282. char *clock;
  283. switch (clockid) {
  284. case CLOCK_BOOTTIME:
  285. clock = "boottime";
  286. break;
  287. case CLOCK_MONOTONIC:
  288. clock = "monotonic";
  289. break;
  290. default:
  291. clock = "unknown";
  292. break;
  293. }
  294. seq_printf(m, "%-10s %10lld %9ld\n", clock, ts->tv_sec, ts->tv_nsec);
  295. }
  296. void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
  297. {
  298. struct ns_common *ns;
  299. struct time_namespace *time_ns;
  300. ns = timens_for_children_get(p);
  301. if (!ns)
  302. return;
  303. time_ns = to_time_ns(ns);
  304. show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
  305. show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
  306. put_time_ns(time_ns);
  307. }
  308. int proc_timens_set_offset(struct file *file, struct task_struct *p,
  309. struct proc_timens_offset *offsets, int noffsets)
  310. {
  311. struct ns_common *ns;
  312. struct time_namespace *time_ns;
  313. struct timespec64 tp;
  314. int i, err;
  315. ns = timens_for_children_get(p);
  316. if (!ns)
  317. return -ESRCH;
  318. time_ns = to_time_ns(ns);
  319. if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
  320. put_time_ns(time_ns);
  321. return -EPERM;
  322. }
  323. for (i = 0; i < noffsets; i++) {
  324. struct proc_timens_offset *off = &offsets[i];
  325. switch (off->clockid) {
  326. case CLOCK_MONOTONIC:
  327. ktime_get_ts64(&tp);
  328. break;
  329. case CLOCK_BOOTTIME:
  330. ktime_get_boottime_ts64(&tp);
  331. break;
  332. default:
  333. err = -EINVAL;
  334. goto out;
  335. }
  336. err = -ERANGE;
  337. if (off->val.tv_sec > KTIME_SEC_MAX ||
  338. off->val.tv_sec < -KTIME_SEC_MAX)
  339. goto out;
  340. tp = timespec64_add(tp, off->val);
  341. /*
  342. * KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
  343. * still unreachable.
  344. */
  345. if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
  346. goto out;
  347. }
  348. mutex_lock(&offset_lock);
  349. if (time_ns->frozen_offsets) {
  350. err = -EACCES;
  351. goto out_unlock;
  352. }
  353. err = 0;
  354. /* Don't report errors after this line */
  355. for (i = 0; i < noffsets; i++) {
  356. struct proc_timens_offset *off = &offsets[i];
  357. struct timespec64 *offset = NULL;
  358. switch (off->clockid) {
  359. case CLOCK_MONOTONIC:
  360. offset = &time_ns->offsets.monotonic;
  361. break;
  362. case CLOCK_BOOTTIME:
  363. offset = &time_ns->offsets.boottime;
  364. break;
  365. }
  366. *offset = off->val;
  367. }
  368. out_unlock:
  369. mutex_unlock(&offset_lock);
  370. out:
  371. put_time_ns(time_ns);
  372. return err;
  373. }
  374. const struct proc_ns_operations timens_operations = {
  375. .name = "time",
  376. .type = CLONE_NEWTIME,
  377. .get = timens_get,
  378. .put = timens_put,
  379. .install = timens_install,
  380. .owner = timens_owner,
  381. };
  382. const struct proc_ns_operations timens_for_children_operations = {
  383. .name = "time_for_children",
  384. .real_ns_name = "time",
  385. .type = CLONE_NEWTIME,
  386. .get = timens_for_children_get,
  387. .put = timens_put,
  388. .install = timens_install,
  389. .owner = timens_owner,
  390. };
  391. struct time_namespace init_time_ns = {
  392. .kref = KREF_INIT(3),
  393. .user_ns = &init_user_ns,
  394. .ns.inum = PROC_TIME_INIT_INO,
  395. .ns.ops = &timens_operations,
  396. .frozen_offsets = true,
  397. };
  398. static int __init time_ns_init(void)
  399. {
  400. return 0;
  401. }
  402. subsys_initcall(time_ns_init);