Skip to content

Commit eba5f96

Browse files
authored
Merge pull request HackTricks-wiki#1213 from HackTricks-wiki/research_update_src_linux-hardening_privilege-escalation_docker-security_namespaces_time-namespace_20250730_082849
Research Update Enhanced src/linux-hardening/privilege-escal...
2 parents 1733276 + cf9372e commit eba5f96

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

‎src/linux-hardening/privilege-escalation/docker-security/namespaces/time-namespace.md‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,92 @@ sudo find /proc -maxdepth 3 -type l -name time -exec ls -l {} \; 2>/dev/null |
6969
nsenter -T TARGET_PID --pid /bin/bash
7070
```
7171

72+
73+
## Manipulating Time Offsets
74+
75+
Starting with Linux 5.6, two clocks can be virtualised per time namespace:
76+
77+
* `CLOCK_MONOTONIC`
78+
* `CLOCK_BOOTTIME`
79+
80+
Their per-namespace deltas are exposed (and can be modified) through the file `/proc/<PID>/timens_offsets`:
81+
82+
```
83+
$ sudo unshare -Tr --mount-proc bash # -T creates a new timens, -r drops capabilities
84+
$ cat /proc/$$/timens_offsets
85+
monotonic 0
86+
boottime 0
87+
```
88+
89+
The file contains two lines – one per clock – with the offset in **nanoseconds**. Processes that hold **CAP_SYS_TIME** _in the time namespace_ can change the value:
90+
91+
```
92+
# advance CLOCK_MONOTONIC by two days (172 800 s)
93+
echo "monotonic 172800000000000" > /proc/$$/timens_offsets
94+
# verify
95+
$ cat /proc/$$/uptime # first column uses CLOCK_MONOTONIC
96+
172801.37 13.57
97+
```
98+
99+
If you need the wall clock (`CLOCK_REALTIME`) to change as well you still have to rely on classic mechanisms (`date`, `hwclock`, `chronyd`, …); it is **not** namespaced.
100+
101+
102+
### `unshare(1)` helper flags (util-linux ≥ 2.38)
103+
104+
```
105+
sudo unshare -T \
106+
--monotonic="+24h" \
107+
--boottime="+7d" \
108+
--mount-proc \
109+
bash
110+
```
111+
112+
The long options automatically write the chosen deltas to `timens_offsets` right after the namespace is created, saving a manual `echo`.
113+
114+
---
115+
116+
## OCI & Runtime support
117+
118+
* The **OCI Runtime Specification v1.1** (Nov 2023) added a dedicated `time` namespace type and the `linux.timeOffsets` field so that container engines can request time virtualisation in a portable way.
119+
* **runc >= 1.2.0** implements that part of the spec. A minimal `config.json` fragment looks like:
120+
```json
121+
{
122+
"linux": {
123+
"namespaces": [
124+
{"type": "time"}
125+
],
126+
"timeOffsets": {
127+
"monotonic": 86400,
128+
"boottime": 600
129+
}
130+
}
131+
}
132+
```
133+
Then run the container with `runc run <id>`.
134+
135+
> NOTE: runc **1.2.6** (Feb 2025) fixed an "exec into container with private timens" bug that could lead to a hang and potential DoS. Make sure you are on ≥ 1.2.6 in production.
136+
137+
---
138+
139+
## Security considerations
140+
141+
1. **Required capability** – A process needs **CAP_SYS_TIME** inside its user/time namespace to change the offsets. Dropping that capability in the container (default in Docker & Kubernetes) prevents tampering.
142+
2. **No wall-clock changes** – Because `CLOCK_REALTIME` is shared with the host, attackers cannot spoof certificate lifetimes, JWT expiry, etc. via timens alone.
143+
3. **Log / detection evasion** – Software that relies on `CLOCK_MONOTONIC` (e.g. rate-limiters based on uptime) can be confused if the namespace user adjusts the offset. Prefer `CLOCK_REALTIME` for security-relevant timestamps.
144+
4. **Kernel attack surface** – Even with `CAP_SYS_TIME` removed, the kernel code remains accessible; keep the host patched. Linux 5.6 → 5.12 received multiple timens bug-fixes (NULL-deref, signedness issues).
145+
146+
### Hardening checklist
147+
148+
* Drop `CAP_SYS_TIME` in your container runtime default profile.
149+
* Keep runtimes updated (runc ≥ 1.2.6, crun ≥ 1.12).
150+
* Pin util-linux ≥ 2.38 if you rely on the `--monotonic/--boottime` helpers.
151+
* Audit in-container software that reads **uptime** or **CLOCK_MONOTONIC** for security-critical logic.
152+
153+
## References
154+
155+
* man7.org – Time namespaces manual page: <https://man7.org/linux/man-pages/man7/time_namespaces.7.html>
156+
* OCI blog – "OCI v1.1: new time and RDT namespaces" (Nov 15 2023): <https://opencontainers.org/blog/2023/11/15/oci-spec-v1.1>
157+
72158
{{#include ../../../../banners/hacktricks-training.md}}
73159

74160

0 commit comments

Comments
 (0)