Skip to content

Commit

Permalink
drgn.internal.sudohelper: pass privileged command as string instead o…
Browse files Browse the repository at this point in the history
…f path

At Meta, our Python executables are packaged in a way that doesn't leave
the original .py or .pyc files easily accessible or importable. This
makes the sudohelper fail with:

  can't open file '/proc/self/fd/3/drgn/internal/sudohelper.pyc': [Errno 2] No such file or directory

We can work around this by passing the helper command as a string
literal. It's ugly, but it works with our Meta package and with
traditional installations.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Jul 19, 2024
1 parent 7e8c5d7 commit f8168da
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions drgn/internal/sudohelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

"""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
Expand All @@ -12,6 +11,31 @@
import tempfile
from typing import Union

_OPEN_VIA_SUDO_COMMAND = r"""
import array
import os
import pickle
import socket
import sys
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)
fds = array.array("i", [fd])
sock.sendmsg(
[b"success"],
[(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)],
)
except Exception as e:
sock.sendmsg([pickle.dumps(e)])
"""


def open_via_sudo(
path: Union[Path, str],
Expand All @@ -31,7 +55,8 @@ def open_via_sudo(
f"[sudo] password for %p to open {path}: ",
sys.executable,
"-B",
__file__,
"-c",
_OPEN_VIA_SUDO_COMMAND,
sockpath,
path,
str(flags),
Expand All @@ -48,26 +73,3 @@ def open_via_sudo(
fds.frombytes(data)
return fds[0]
raise pickle.loads(msg)


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)
fds = array.array("i", [fd])
sock.sendmsg(
[b"success"],
[(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)],
)
except Exception as e:
sock.sendmsg([pickle.dumps(e)])


if __name__ == "__main__":
main()

0 comments on commit f8168da

Please sign in to comment.