blob: 89f83f426df8c0b8aa5c3b0e092e275209effb0c [file] [log] [blame]
Andrew Geissler615f2f12022-07-15 14:00:58 -05001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
3# Script used for running executables with custom labels, as well as custom uid/gid
4# Process label is changed by writing to /proc/self/attr/curent
5#
6# Script expects user id and group id to exist, and be the same.
7#
8# From adduser manual:
9# """By default, each user in Debian GNU/Linux is given a corresponding group
10# with the same name. """
11#
Andrew Geissler615f2f12022-07-15 14:00:58 -050012# Usage: root@desk:~# python3 notroot.py <uid> <label> <full_path_to_executable> [arguments ..]
13# eg: python3 notroot.py 1000 User::Label /bin/ping -c 3 192.168.1.1
Brad Bishopc342db32019-05-15 21:57:59 -040014#
15# Author: Alexandru Cornea <alexandru.cornea@intel.com>
16import os
17import sys
18
19try:
20 uid = int(sys.argv[1])
21 sys.argv.pop(1)
22 label = sys.argv[1]
23 sys.argv.pop(1)
24 open("/proc/self/attr/current", "w").write(label)
25 path=sys.argv[1]
26 sys.argv.pop(0)
27 os.setgid(uid)
28 os.setuid(uid)
29 os.execv(path,sys.argv)
30
Andrew Geissler615f2f12022-07-15 14:00:58 -050031except Exception as e:
32 print(e.strerror)
33 sys.exit(-1)