Skip to content

Commit 04a8682

Browse files
avaginKAGA-KOKO
authored andcommitted
fs/proc: Introduce /proc/pid/timens_offsets
API to set time namespace offsets for children processes, i.e.: echo "$clockid $offset_sec $offset_nsec" > /proc/self/timens_offsets Co-developed-by: Dmitry Safonov <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 70ddf65 commit 04a8682

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed

fs/proc/base.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include <linux/sched/debug.h>
9595
#include <linux/sched/stat.h>
9696
#include <linux/posix-timers.h>
97+
#include <linux/time_namespace.h>
9798
#include <trace/events/oom.h>
9899
#include "internal.h"
99100
#include "fd.h"
@@ -1533,6 +1534,96 @@ static const struct file_operations proc_pid_sched_autogroup_operations = {
15331534

15341535
#endif /* CONFIG_SCHED_AUTOGROUP */
15351536

1537+
#ifdef CONFIG_TIME_NS
1538+
static int timens_offsets_show(struct seq_file *m, void *v)
1539+
{
1540+
struct task_struct *p;
1541+
1542+
p = get_proc_task(file_inode(m->file));
1543+
if (!p)
1544+
return -ESRCH;
1545+
proc_timens_show_offsets(p, m);
1546+
1547+
put_task_struct(p);
1548+
1549+
return 0;
1550+
}
1551+
1552+
static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
1553+
size_t count, loff_t *ppos)
1554+
{
1555+
struct inode *inode = file_inode(file);
1556+
struct proc_timens_offset offsets[2];
1557+
char *kbuf = NULL, *pos, *next_line;
1558+
struct task_struct *p;
1559+
int ret, noffsets;
1560+
1561+
/* Only allow < page size writes at the beginning of the file */
1562+
if ((*ppos != 0) || (count >= PAGE_SIZE))
1563+
return -EINVAL;
1564+
1565+
/* Slurp in the user data */
1566+
kbuf = memdup_user_nul(buf, count);
1567+
if (IS_ERR(kbuf))
1568+
return PTR_ERR(kbuf);
1569+
1570+
/* Parse the user data */
1571+
ret = -EINVAL;
1572+
noffsets = 0;
1573+
for (pos = kbuf; pos; pos = next_line) {
1574+
struct proc_timens_offset *off = &offsets[noffsets];
1575+
int err;
1576+
1577+
/* Find the end of line and ensure we don't look past it */
1578+
next_line = strchr(pos, '\n');
1579+
if (next_line) {
1580+
*next_line = '\0';
1581+
next_line++;
1582+
if (*next_line == '\0')
1583+
next_line = NULL;
1584+
}
1585+
1586+
err = sscanf(pos, "%u %lld %lu", &off->clockid,
1587+
&off->val.tv_sec, &off->val.tv_nsec);
1588+
if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
1589+
goto out;
1590+
noffsets++;
1591+
if (noffsets == ARRAY_SIZE(offsets)) {
1592+
if (next_line)
1593+
count = next_line - kbuf;
1594+
break;
1595+
}
1596+
}
1597+
1598+
ret = -ESRCH;
1599+
p = get_proc_task(inode);
1600+
if (!p)
1601+
goto out;
1602+
ret = proc_timens_set_offset(file, p, offsets, noffsets);
1603+
put_task_struct(p);
1604+
if (ret)
1605+
goto out;
1606+
1607+
ret = count;
1608+
out:
1609+
kfree(kbuf);
1610+
return ret;
1611+
}
1612+
1613+
static int timens_offsets_open(struct inode *inode, struct file *filp)
1614+
{
1615+
return single_open(filp, timens_offsets_show, inode);
1616+
}
1617+
1618+
static const struct file_operations proc_timens_offsets_operations = {
1619+
.open = timens_offsets_open,
1620+
.read = seq_read,
1621+
.write = timens_offsets_write,
1622+
.llseek = seq_lseek,
1623+
.release = single_release,
1624+
};
1625+
#endif /* CONFIG_TIME_NS */
1626+
15361627
static ssize_t comm_write(struct file *file, const char __user *buf,
15371628
size_t count, loff_t *offset)
15381629
{
@@ -3015,6 +3106,9 @@ static const struct pid_entry tgid_base_stuff[] = {
30153106
#endif
30163107
#ifdef CONFIG_SCHED_AUTOGROUP
30173108
REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3109+
#endif
3110+
#ifdef CONFIG_TIME_NS
3111+
REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
30183112
#endif
30193113
REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
30203114
#ifdef CONFIG_HAVE_ARCH_TRACEHOOK

include/linux/time_namespace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ static inline void put_time_ns(struct time_namespace *ns)
5252
kref_put(&ns->kref, free_time_ns);
5353
}
5454

55+
void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
56+
57+
struct proc_timens_offset {
58+
int clockid;
59+
struct timespec64 val;
60+
};
61+
62+
int proc_timens_set_offset(struct file *file, struct task_struct *p,
63+
struct proc_timens_offset *offsets, int n);
64+
5565
static inline void timens_add_monotonic(struct timespec64 *ts)
5666
{
5767
struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;

kernel/time/namespace.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/user_namespace.h>
99
#include <linux/sched/signal.h>
1010
#include <linux/sched/task.h>
11+
#include <linux/seq_file.h>
1112
#include <linux/proc_ns.h>
1213
#include <linux/export.h>
1314
#include <linux/time.h>
@@ -334,6 +335,106 @@ static struct user_namespace *timens_owner(struct ns_common *ns)
334335
return to_time_ns(ns)->user_ns;
335336
}
336337

338+
static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
339+
{
340+
seq_printf(m, "%d %lld %ld\n", clockid, ts->tv_sec, ts->tv_nsec);
341+
}
342+
343+
void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
344+
{
345+
struct ns_common *ns;
346+
struct time_namespace *time_ns;
347+
348+
ns = timens_for_children_get(p);
349+
if (!ns)
350+
return;
351+
time_ns = to_time_ns(ns);
352+
353+
show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
354+
show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
355+
put_time_ns(time_ns);
356+
}
357+
358+
int proc_timens_set_offset(struct file *file, struct task_struct *p,
359+
struct proc_timens_offset *offsets, int noffsets)
360+
{
361+
struct ns_common *ns;
362+
struct time_namespace *time_ns;
363+
struct timespec64 tp;
364+
int i, err;
365+
366+
ns = timens_for_children_get(p);
367+
if (!ns)
368+
return -ESRCH;
369+
time_ns = to_time_ns(ns);
370+
371+
if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
372+
put_time_ns(time_ns);
373+
return -EPERM;
374+
}
375+
376+
for (i = 0; i < noffsets; i++) {
377+
struct proc_timens_offset *off = &offsets[i];
378+
379+
switch (off->clockid) {
380+
case CLOCK_MONOTONIC:
381+
ktime_get_ts64(&tp);
382+
break;
383+
case CLOCK_BOOTTIME:
384+
ktime_get_boottime_ts64(&tp);
385+
break;
386+
default:
387+
err = -EINVAL;
388+
goto out;
389+
}
390+
391+
err = -ERANGE;
392+
393+
if (off->val.tv_sec > KTIME_SEC_MAX ||
394+
off->val.tv_sec < -KTIME_SEC_MAX)
395+
goto out;
396+
397+
tp = timespec64_add(tp, off->val);
398+
/*
399+
* KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
400+
* still unreachable.
401+
*/
402+
if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
403+
goto out;
404+
}
405+
406+
mutex_lock(&offset_lock);
407+
if (time_ns->frozen_offsets) {
408+
err = -EACCES;
409+
goto out_unlock;
410+
}
411+
412+
err = 0;
413+
/* Don't report errors after this line */
414+
for (i = 0; i < noffsets; i++) {
415+
struct proc_timens_offset *off = &offsets[i];
416+
struct timespec64 *offset = NULL;
417+
418+
switch (off->clockid) {
419+
case CLOCK_MONOTONIC:
420+
offset = &time_ns->offsets.monotonic;
421+
break;
422+
case CLOCK_BOOTTIME:
423+
offset = &time_ns->offsets.boottime;
424+
break;
425+
}
426+
427+
*offset = off->val;
428+
}
429+
430+
out_unlock:
431+
mutex_unlock(&offset_lock);
432+
out:
433+
put_time_ns(time_ns);
434+
435+
return err;
436+
}
437+
337438
const struct proc_ns_operations timens_operations = {
338439
.name = "time",
339440
.type = CLONE_NEWTIME,

0 commit comments

Comments
 (0)