linux-keyctl 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- linux_keyctl/__init__.py +661 -0
- linux_keyctl/_syscall.py +116 -0
- linux_keyctl/py.typed +0 -0
- linux_keyctl-1.1.0.dist-info/METADATA +88 -0
- linux_keyctl-1.1.0.dist-info/RECORD +8 -0
- linux_keyctl-1.1.0.dist-info/WHEEL +5 -0
- linux_keyctl-1.1.0.dist-info/licenses/LICENSE +674 -0
- linux_keyctl-1.1.0.dist-info/top_level.txt +1 -0
linux_keyctl/_syscall.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
# Copyright (C) 2026 Juan Domingo Martín
|
|
3
|
+
"""Raw ``add_key(2)``, ``request_key(2)`` and ``keyctl(2)`` calls.
|
|
4
|
+
|
|
5
|
+
Internal module: callers must validate argument types and ranges
|
|
6
|
+
first. Calls go through libc's variadic ``syscall()`` (ctypes ``CDLL``,
|
|
7
|
+
which releases the GIL during the call). Integer arguments are passed
|
|
8
|
+
as ``c_long``; ``bytes`` as NUL-terminated pointers; ``None`` as NULL.
|
|
9
|
+
|
|
10
|
+
The backend is chosen once at import from a fixed table of syscall
|
|
11
|
+
numbers. On an unsupported platform it is ``None`` and every call
|
|
12
|
+
raises ``OSError`` with ``ENOSYS``. Import makes no system call
|
|
13
|
+
through the backend.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import ctypes
|
|
17
|
+
import errno
|
|
18
|
+
import os
|
|
19
|
+
import platform
|
|
20
|
+
import sys
|
|
21
|
+
|
|
22
|
+
# (add_key, request_key, keyctl) for 64-bit processes, from the Linux
|
|
23
|
+
# UAPI headers (asm/unistd_64.h and asm-generic/unistd.h). Never guess
|
|
24
|
+
# these: a wrong number runs an unrelated system call.
|
|
25
|
+
_SYSCALL_NUMBERS = {
|
|
26
|
+
"x86_64": (248, 249, 250),
|
|
27
|
+
"aarch64": (217, 218, 219),
|
|
28
|
+
"riscv64": (217, 218, 219),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
# keyctl(2) operations.
|
|
32
|
+
KEYCTL_JOIN_SESSION_KEYRING = 1
|
|
33
|
+
KEYCTL_UPDATE = 2
|
|
34
|
+
KEYCTL_REVOKE = 3
|
|
35
|
+
KEYCTL_SETPERM = 5
|
|
36
|
+
KEYCTL_DESCRIBE = 6
|
|
37
|
+
KEYCTL_CLEAR = 7
|
|
38
|
+
KEYCTL_LINK = 8
|
|
39
|
+
KEYCTL_UNLINK = 9
|
|
40
|
+
KEYCTL_SEARCH = 10
|
|
41
|
+
KEYCTL_READ = 11
|
|
42
|
+
KEYCTL_SET_TIMEOUT = 15
|
|
43
|
+
KEYCTL_SESSION_TO_PARENT = 18
|
|
44
|
+
KEYCTL_INVALIDATE = 21
|
|
45
|
+
KEYCTL_MOVE = 30
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _load():
|
|
49
|
+
"""Return ``(syscall, numbers)``, or ``None`` if unusable here."""
|
|
50
|
+
if not sys.platform.startswith("linux"):
|
|
51
|
+
return None
|
|
52
|
+
if ctypes.sizeof(ctypes.c_void_p) != 8:
|
|
53
|
+
return None
|
|
54
|
+
numbers = _SYSCALL_NUMBERS.get(platform.machine())
|
|
55
|
+
if numbers is None:
|
|
56
|
+
return None
|
|
57
|
+
try:
|
|
58
|
+
func = ctypes.CDLL(None, use_errno=True).syscall
|
|
59
|
+
except (OSError, AttributeError):
|
|
60
|
+
return None
|
|
61
|
+
func.restype = ctypes.c_long
|
|
62
|
+
return func, numbers
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Immutable after import; tests may replace it to simulate an
|
|
66
|
+
# unusable backend.
|
|
67
|
+
_BACKEND = _load()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _arg(value):
|
|
71
|
+
if isinstance(value, int):
|
|
72
|
+
return ctypes.c_long(value)
|
|
73
|
+
return value # bytes, None or a ctypes buffer
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _call(index, *args):
|
|
77
|
+
"""Make system call ``index`` (0 add_key, 1 request_key, 2 keyctl).
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
The non-negative result.
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
OSError: The call returned -1 (errno from ``ctypes.get_errno``),
|
|
84
|
+
or the backend is unavailable (``ENOSYS``).
|
|
85
|
+
"""
|
|
86
|
+
backend = _BACKEND
|
|
87
|
+
if backend is None:
|
|
88
|
+
raise OSError(errno.ENOSYS, os.strerror(errno.ENOSYS))
|
|
89
|
+
func, numbers = backend
|
|
90
|
+
result = func(ctypes.c_long(numbers[index]), *[_arg(a) for a in args])
|
|
91
|
+
if result == -1:
|
|
92
|
+
err = ctypes.get_errno()
|
|
93
|
+
raise OSError(err, os.strerror(err))
|
|
94
|
+
return result
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def add_key(key_type, description, payload, keyring):
|
|
98
|
+
"""Call ``add_key(2)``; the payload length is ``len(payload)``."""
|
|
99
|
+
return _call(
|
|
100
|
+
0,
|
|
101
|
+
key_type,
|
|
102
|
+
description,
|
|
103
|
+
payload,
|
|
104
|
+
ctypes.c_size_t(len(payload)),
|
|
105
|
+
keyring,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def request_key(key_type, description, callout_info, keyring):
|
|
110
|
+
"""Call ``request_key(2)``."""
|
|
111
|
+
return _call(1, key_type, description, callout_info, keyring)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def keyctl(op, arg2=0, arg3=0, arg4=0, arg5=0):
|
|
115
|
+
"""Call ``keyctl(2)``; unused arguments are passed as ``0``."""
|
|
116
|
+
return _call(2, op, arg2, arg3, arg4, arg5)
|
linux_keyctl/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: linux-keyctl
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Thin Python interface to the Linux kernel key management system calls
|
|
5
|
+
Author: Juan Domingo Martín
|
|
6
|
+
License-Expression: GPL-3.0-or-later
|
|
7
|
+
Project-URL: Homepage, https://github.com/juando-martin/linux-keyctl
|
|
8
|
+
Project-URL: Documentation, https://linux-keyctl.readthedocs.io/
|
|
9
|
+
Project-URL: Source, https://github.com/juando-martin/linux-keyctl
|
|
10
|
+
Project-URL: Issues, https://github.com/juando-martin/linux-keyctl/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/juando-martin/linux-keyctl/blob/main/CHANGES.md
|
|
12
|
+
Keywords: linux,keyring,keyctl,keyutils,kernel,secrets
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Security
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Topic :: System :: Operating System Kernels :: Linux
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Provides-Extra: docs
|
|
29
|
+
Requires-Dist: sphinx<10,>=8; extra == "docs"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: black<27,>=26; extra == "dev"
|
|
32
|
+
Requires-Dist: pycodestyle<3,>=2.14; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# linux-keyctl
|
|
36
|
+
|
|
37
|
+
A thin Python interface to the Linux kernel key management facility
|
|
38
|
+
(`add_key(2)`, `request_key(2)`, `keyctl(2)`).
|
|
39
|
+
|
|
40
|
+
- Pure Python: calls the system calls directly through `ctypes`; no
|
|
41
|
+
`libkeyutils`, no compiled extension, no dependencies.
|
|
42
|
+
- One function per `libkeyutils` operation, with the kernel's semantics.
|
|
43
|
+
- Kernel failures raise `OSError` with the kernel's `errno`.
|
|
44
|
+
- Secrets are never logged, cached or put in exception messages.
|
|
45
|
+
- Python ≥ 3.11; 64-bit Linux on x86_64, aarch64 or riscv64.
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
pip install linux-keyctl
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The import name is `linux_keyctl`. (The unrelated PyPI package `keyctl`
|
|
54
|
+
uses the import name `keyctl`; this one never installs it.)
|
|
55
|
+
|
|
56
|
+
## Example
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import linux_keyctl as keyctl
|
|
60
|
+
|
|
61
|
+
SESSION = keyctl.KEY_SPEC_SESSION_KEYRING
|
|
62
|
+
|
|
63
|
+
key = keyctl.add_key(b"my-app:token", b"s3cret", SESSION)
|
|
64
|
+
keyctl.set_timeout(key, 3600) # expires in one hour
|
|
65
|
+
assert keyctl.read_key(key) == b"s3cret"
|
|
66
|
+
assert keyctl.search(SESSION, b"my-app:token") == key
|
|
67
|
+
keyctl.revoke(key)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Descriptions, key types and payloads are `bytes`. Importing the package
|
|
71
|
+
never touches the keyring; on a platform without keyring support every
|
|
72
|
+
call raises `OSError` with `errno.ENOSYS`.
|
|
73
|
+
|
|
74
|
+
## Documentation
|
|
75
|
+
|
|
76
|
+
- [Manual](https://linux-keyctl.readthedocs.io/): getting started,
|
|
77
|
+
integration contracts, API reference.
|
|
78
|
+
- [Specification](https://github.com/juando-martin/linux-keyctl/blob/main/SPECIFICATIONS.md):
|
|
79
|
+
the complete contract.
|
|
80
|
+
- [Changes](https://github.com/juando-martin/linux-keyctl/blob/main/CHANGES.md)
|
|
81
|
+
|
|
82
|
+
Development setup, tests and the documentation build are described in the
|
|
83
|
+
[contributing guide](https://linux-keyctl.readthedocs.io/en/latest/contributing.html).
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
GPL-3.0-or-later. See
|
|
88
|
+
[LICENSE](https://github.com/juando-martin/linux-keyctl/blob/main/LICENSE).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
linux_keyctl/__init__.py,sha256=4HQiIUUpidW4-1uRilNFEXUCxPLwX4w-Hm4j8JlaOxc,18106
|
|
2
|
+
linux_keyctl/_syscall.py,sha256=jfNrM5rJm78fUbjYnwwibnQhwIuKGcgNzOgimyIvJNQ,3316
|
|
3
|
+
linux_keyctl/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
linux_keyctl-1.1.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
5
|
+
linux_keyctl-1.1.0.dist-info/METADATA,sha256=R4BRbmM9IskX-NsFt6OjUy4rRxI4ITebZPXihbJUDFM,3337
|
|
6
|
+
linux_keyctl-1.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
linux_keyctl-1.1.0.dist-info/top_level.txt,sha256=uRUJj5Ur3KnwAqXF92mTG19Eo5szd4G14DR9FUnG7jg,13
|
|
8
|
+
linux_keyctl-1.1.0.dist-info/RECORD,,
|