namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_ACCOUNT);
  81. if (!ns)
  82. goto fail_dec;
  83. refcount_set(&ns->ns.count, 1);
  84. ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __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. struct page *find_timens_vvar_page(struct vm_area_struct *vma)
  172. {
  173. if (likely(vma->vm_mm == current->mm))
  174. return current->nsproxy->time_ns->vvar_page;
  175. /*
  176. * VM_PFNMAP | VM_IO protect .fault() handler from being called
  177. * through interfaces like /proc/$pid/mem or
  178. * process_vm_{readv,writev}() as long as there's no .access()
  179. * in special_mapping_vmops().
  180. * For more details check_vma_flags() and __access_remote_vm()
  181. */
  182. WARN(1, "vvar_page accessed remotely");
  183. return NULL;
  184. }
  185. /*
  186. * Protects possibly multiple offsets writers racing each other
  187. * and tasks entering the namespace.
  188. */
  189. static DEFINE_MUTEX(offset_lock);
  190. static void timens_set_vvar_page(struct task_struct *task,
  191. struct time_namespace *ns)
  192. {
  193. struct vdso_data *vdata;
  194. unsigned int i;
  195. if (ns == &init_time_ns)
  196. return;
  197. /* Fast-path, taken by every task in namespace except the first. */
  198. if (likely(ns->frozen_offsets))
  199. return;
  200. mutex_lock(&offset_lock);
  201. /* Nothing to-do: vvar_page has been already initialized. */
  202. if (ns->frozen_offsets)
  203. goto out;
  204. ns->frozen_offsets = true;
  205. vdata = arch_get_vdso_data(page_address(ns->vvar_page));
  206. for (i = 0; i < CS_BASES; i++)
  207. timens_setup_vdso_data(&vdata[i], ns);
  208. out:
  209. mutex_unlock(&offset_lock);
  210. }
  211. void free_time_ns(struct time_namespace *ns)
  212. {
  213. dec_time_namespaces(ns->ucounts);
  214. put_user_ns(ns->user_ns);
  215. ns_free_inum(&ns->ns);
  216. __free_page(ns->vvar_page);
  217. kfree(ns);
  218. }
  219. static struct time_namespace *to_time_ns(struct ns_common *ns)
  220. {
  221. return container_of(ns, struct time_namespace, ns);
  222. }
  223. static struct ns_common *timens_get(struct task_struct *task)
  224. {
  225. struct time_namespace *ns = NULL;
  226. struct nsproxy *nsproxy;
  227. task_lock(task);
  228. nsproxy = task->nsproxy;
  229. if (nsproxy) {
  230. ns = nsproxy->time_ns;
  231. get_time_ns(ns);
  232. }
  233. task_unlock(task);
  234. return ns ? &ns->ns : NULL;
  235. }
  236. static struct ns_common *timens_for_children_get(struct task_struct *task)
  237. {
  238. struct time_namespace *ns = NULL;
  239. struct nsproxy *nsproxy;
  240. task_lock(task);
  241. nsproxy = task->nsproxy;
  242. if (nsproxy) {
  243. ns = nsproxy->time_ns_for_children;
  244. get_time_ns(ns);
  245. }
  246. task_unlock(task);
  247. return ns ? &ns->ns : NULL;
  248. }
  249. static void timens_put(struct ns_common *ns)
  250. {
  251. put_time_ns(to_time_ns(ns));
  252. }
  253. void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
  254. {
  255. timens_set_vvar_page(tsk, ns);
  256. vdso_join_timens(tsk, ns);
  257. }
  258. static int timens_install(struct nsset *nsset, struct ns_common *new)
  259. {
  260. struct nsproxy *nsproxy = nsset->nsproxy;
  261. struct time_namespace *ns = to_time_ns(new);
  262. if (!current_is_single_threaded())
  263. return -EUSERS;
  264. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  265. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  266. return -EPERM;
  267. get_time_ns(ns);
  268. put_time_ns(nsproxy->time_ns);
  269. nsproxy->time_ns = ns;
  270. get_time_ns(ns);
  271. put_time_ns(nsproxy->time_ns_for_children);
  272. nsproxy->time_ns_for_children = ns;
  273. return 0;
  274. }
  275. void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
  276. {
  277. struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
  278. struct time_namespace *ns = to_time_ns(nsc);
  279. /* create_new_namespaces() already incremented the ref counter */
  280. if (nsproxy->time_ns == nsproxy->time_ns_for_children)
  281. return;
  282. get_time_ns(ns);
  283. put_time_ns(nsproxy->time_ns);
  284. nsproxy->time_ns = ns;
  285. timens_commit(tsk, ns);
  286. }
  287. static struct user_namespace *timens_owner(struct ns_common *ns)
  288. {
  289. return to_time_ns(ns)->user_ns;
  290. }
  291. static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
  292. {
  293. char *clock;
  294. switch (clockid) {
  295. case CLOCK_BOOTTIME:
  296. clock = "boottime";
  297. break;
  298. case CLOCK_MONOTONIC:
  299. clock = "monotonic";
  300. break;
  301. default:
  302. clock = "unknown";
  303. break;
  304. }
  305. seq_printf(m, "%-10s %10lld %9ld\n", clock, ts->tv_sec, ts->tv_nsec);
  306. }
  307. void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
  308. {
  309. struct ns_common *ns;
  310. struct time_namespace *time_ns;
  311. ns = timens_for_children_get(p);
  312. if (!ns)
  313. return;
  314. time_ns = to_time_ns(ns);
  315. show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
  316. show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
  317. put_time_ns(time_ns);
  318. }
  319. int proc_timens_set_offset(struct file *file, struct task_struct *p,
  320. struct proc_timens_offset *offsets, int noffsets)
  321. {
  322. struct ns_common *ns;
  323. struct time_namespace *time_ns;
  324. struct timespec64 tp;
  325. int i, err;
  326. ns = timens_for_children_get(p);
  327. if (!ns)
  328. return -ESRCH;
  329. time_ns = to_time_ns(ns);
  330. if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
  331. put_time_ns(time_ns);
  332. return -EPERM;
  333. }
  334. for (i = 0; i < noffsets; i++) {
  335. struct proc_timens_offset *off = &offsets[i];
  336. switch (off->clockid) {
  337. case CLOCK_MONOTONIC:
  338. ktime_get_ts64(&tp);
  339. break;
  340. case CLOCK_BOOTTIME:
  341. ktime_get_boottime_ts64(&tp);
  342. break;
  343. default:
  344. err = -EINVAL;
  345. goto out;
  346. }
  347. err = -ERANGE;
  348. if (off->val.tv_sec > KTIME_SEC_MAX ||
  349. off->val.tv_sec < -KTIME_SEC_MAX)
  350. goto out;
  351. tp = timespec64_add(tp, off->val);
  352. /*
  353. * KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
  354. * still unreachable.
  355. */
  356. if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
  357. goto out;
  358. }
  359. mutex_lock(&offset_lock);
  360. if (time_ns->frozen_offsets) {
  361. err = -EACCES;
  362. goto out_unlock;
  363. }
  364. err = 0;
  365. /* Don't report errors after this line */
  366. for (i = 0; i < noffsets; i++) {
  367. struct proc_timens_offset *off = &offsets[i];
  368. struct timespec64 *offset = NULL;
  369. switch (off->clockid) {
  370. case CLOCK_MONOTONIC:
  371. offset = &time_ns->offsets.monotonic;
  372. break;
  373. case CLOCK_BOOTTIME:
  374. offset = &time_ns->offsets.boottime;
  375. break;
  376. }
  377. *offset = off->val;
  378. }
  379. out_unlock:
  380. mutex_unlock(&offset_lock);
  381. out:
  382. put_time_ns(time_ns);
  383. return err;
  384. }
  385. const struct proc_ns_operations timens_operations = {
  386. .name = "time",
  387. .type = CLONE_NEWTIME,
  388. .get = timens_get,
  389. .put = timens_put,
  390. .install = timens_install,
  391. .owner = timens_owner,
  392. };
  393. const struct proc_ns_operations timens_for_children_operations = {
  394. .name = "time_for_children",
  395. .real_ns_name = "time",
  396. .type = CLONE_NEWTIME,
  397. .get = timens_for_children_get,
  398. .put = timens_put,
  399. .install = timens_install,
  400. .owner = timens_owner,
  401. };
  402. struct time_namespace init_time_ns = {
  403. .ns.count = REFCOUNT_INIT(3),
  404. .user_ns = &init_user_ns,
  405. .ns.inum = PROC_TIME_INIT_INO,
  406. .ns.ops = &timens_operations,
  407. .frozen_offsets = true,
  408. };