-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Open /proc/kcore via sudo when not root
Non-root users can now run Drgn against the running kernel. Drgn will attempt to use sudo to open /proc/kcore and transmit the opened file descriptor back to the user process. The file descriptor is then passed to Program.set_fd(). The user must still have sudo privileges. Signed-off-by: Stephen Brennan <[email protected]>
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright (c) Stephen Brennan <[email protected]> | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
"""Helper for opening a file as root and transmitting it via unix socket""" | ||
import array | ||
import os | ||
from pathlib import Path | ||
import pickle | ||
import socket | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from typing import Union | ||
|
||
|
||
def recv_fd(sock: socket.socket) -> int: | ||
"""Receive a file descripter on the socket""" | ||
# Borrows heavily from the Python docs for sock.recvmsg() | ||
fds = array.array("i") | ||
msg, ancdata, flags, addr = sock.recvmsg(4096, socket.CMSG_SPACE(fds.itemsize)) | ||
if msg == b"success" and ancdata: | ||
level, typ, data = ancdata[0] | ||
assert level == socket.SOL_SOCKET | ||
assert typ == socket.SCM_RIGHTS | ||
data = data[: fds.itemsize] | ||
fds.frombytes(data) | ||
return fds[0] | ||
else: | ||
try: | ||
exc = pickle.loads(msg) | ||
except Exception: | ||
exc = Exception(msg.decode()) | ||
raise exc | ||
|
||
|
||
def send_fd(sock: socket.socket, fd: int) -> None: | ||
"""Send a file descriptor on the socket""" | ||
# Borrows heavily from the Python docs for sock.sendmsg() | ||
fds = array.array("i", [fd]) | ||
sock.sendmsg( | ||
[b"success"], | ||
[(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)], | ||
) | ||
|
||
|
||
def open_via_sudo( | ||
path: Union[Path, str], | ||
flags: int, | ||
mode: int = 0o777, | ||
) -> int: | ||
"""Implements os.open() using sudo to get permissions""" | ||
# Currently does not support dir_fd argument | ||
with tempfile.TemporaryDirectory() as td: | ||
sockpath = Path(td) / "sock" | ||
with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as sock: | ||
sock.bind(str(sockpath)) | ||
subprocess.check_call( | ||
[ | ||
"sudo", | ||
sys.executable, | ||
"-B", | ||
__file__, | ||
sockpath, | ||
path, | ||
str(flags), | ||
str(mode), | ||
], | ||
) | ||
return recv_fd(sock) | ||
|
||
|
||
def main() -> None: | ||
sockpath = sys.argv[1] | ||
filename = sys.argv[2] | ||
flags = int(sys.argv[3]) | ||
mode = int(sys.argv[4]) | ||
|
||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | ||
sock.connect(sockpath) | ||
try: | ||
fd = os.open(filename, flags, mode) | ||
send_fd(sock, fd) | ||
except Exception as e: | ||
sock.sendmsg([pickle.dumps(e)]) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |