File size: 1,158 Bytes
8fc7ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# SPDX-License-Identifier: Apache-2.0
"""Fixed child used only to attest a fresh Linux network namespace.

The parent launches this file through ``unshare --net``.  It performs no
network operation and accepts no command, expression, or package input.
"""

import json
import os
import sys
from pathlib import Path


def main() -> int:
    if len(sys.argv) != 2:
        return 64
    target = Path(sys.argv[1])
    interfaces = sorted(path.name for path in Path("/sys/class/net").iterdir())
    loopback_state = None
    state_path = Path("/sys/class/net/lo/operstate")
    if state_path.is_file():
        loopback_state = state_path.read_text(encoding="utf-8").strip()
    payload = {
        "schema": "szl.numerics.network-namespace-evidence/v1",
        "network_operations_performed": 0,
        "network_namespace": os.readlink("/proc/self/ns/net"),
        "interfaces": interfaces,
        "loopback_operstate": loopback_state,
    }
    target.write_text(
        json.dumps(payload, sort_keys=True, separators=(",", ":"), ensure_ascii=False),
        encoding="utf-8",
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())