c - Getting the environment of a process running under a different user -


assume have process pid 1234 running in background under user a.

if run following program user a, succeeds. if run user b, fails open: permission denied.

this makes sense, environ file owned user , has read permission a. if make program set-user-id user , run user b, fails read: permission denied. doesn't seem happen regular file having same permissions. doesn't happen if root.

any ideas why? there other way environment of process works around issue?

#include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h>  int main(int argc, const char *argv[]) {     unsigned char ch = 0;     int fd = -1;     int read_result = -1;      setresuid(geteuid(), geteuid(), geteuid());      fd = open("/proc/1234/environ", o_rdonly);     if (-1 == fd) {         perror("open");         return exit_failure;     }      read_result = read(fd, &ch, 1);     if (-1 == read_result) {         perror("read");         return exit_failure;     }      close(fd);      return exit_success; } 

as can see, if program run without setuid, open(2) gives permission denied, whereas if run program with setuid, open(2) works ok, read(2) causes same error. happens because of additional permission check during each file operation on /proc/* inodes. looks additional permission check uses other euid of running process. if run gnu/linux, more details see note @ beginning of code in <kernel_source>/fs/proc/base.c , environ_read() function in same file.

one of possible quick solutions:

  • set owner of program file root
  • set owner group special group
  • add user should run program (user b) special group
  • set mode bits 4550 (r-sr-x---)
  • call setuid(getuid()) drop priveleges possible, i.e. right after reading environ file

in case user given group read /proc/*/environ of any other user.

if want reduce permissions of program allow read environ files of specific user (user a), should think of other tricks. example config file, containing user(s) environ file(s) read.

always careful permissions. root permissions. necessary privileged operations , drop permissions possible.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -