Why does a process of a binary with only execute permission remain hidden in "ps" when using hidepid=2, if the user is not root?

rsuarez

I'm using hidepid=2 to mount /proc, so users can't see any but their own processes. A particular binary I want to use has been restricted to rwx--x--x permissions, so only its owner (root) can read it but other users can execute it. A normal user can run it without problems, but can't see the process with "ps". If the binary has its permissions changed so the user can read it, then the process appears in "ps" again.

A reproducible example:

sudo mount -o remount,hidepid=2 /proc
sudo cp $(which yes) /tmp
sudo chmod 0711 /tmp/yes
/tmp/yes >/dev/null &
ps aux | grep yes # The process is hidden
sudo ps aux | grep yes # The process can be seen by root
kill %1
sudo chmod og+r /tmp/yes
/tmp/yes >/dev/null &
ps aux | grep yes # The process appears in the list

Why is this happening? It obviously has some relationship to the file permissions, but it shouldn't have: if the process belongs to a user, the user should be able to see it even if the binary is restricted.

My guess is that, as the link "exe" inside /proc/PID points to the binary being executed, the kernel is forbidding all access the the directory in addition to the binary itself. But I'd like to know if this is true or just a consequence of some other thing going on.

Thanks in advance!

mr.spuratic

The answer is (or at least starts) in fs/proc/base.c (unchanged from kernel 3.12 to 4.2 at least)

742 static int proc_pid_permission(struct inode *inode, int mask)
743 {
744         struct pid_namespace *pid = inode->i_sb->s_fs_info;
745         struct task_struct *task;
746         bool has_perms;
747 
748         task = get_proc_task(inode);
749         if (!task)
750                 return -ESRCH;
751         has_perms = has_pid_permissions(pid, task, 1);
752         put_task_struct(task);
753 
754         if (!has_perms) {
755                 if (pid->hide_pid == 2) {
756                         /*
757                          * Let's make getdents(), stat(), and open()
758                          * consistent with each other.  If a process
759                          * may not stat() a file, it shouldn't be    seen
760                          * in procfs at all.
761                          */
762                         return -ENOENT;
763                 }
764 
765                 return -EPERM;
766         }
767         return generic_permission(inode, mask);
768 }

The code above is the starting point for determining if a specific /proc/PID entry can been seen to exist or not. When hide_pid is set to 2 it returns -ENOENT if you don't have the required permission. Permissions are checked via:

has_pid_permissions()ptrace_may_access()__ptrace_may_access()

__ptrace_may_access() denies access because the process is not "dumpable" as it was created from an unreadable executable image, as determined during process creation:

setup_new_exec()would_dump()

1118 void would_dump(struct linux_binprm *bprm, struct file *file)
1119 {
1120         if (inode_permission(file_inode(file), MAY_READ) < 0)
1121                 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1122 }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does (ps -f) create no subshell but a separate process?

분류에서Dev

Why a folder needs a execute permission when I'm trying to execute a .sh?

분류에서Dev

Binary Search Tree Traversals failed to execute when using recursion

분류에서Dev

Why does initramfs mount the root filesystem read-only

분류에서Dev

Why does Tk().after only execute once and how to fix it?

분류에서Dev

process Count of each user in ps aux command

분류에서Dev

Why does ssh look for keys in /root/.ssh when run with sudo?

분류에서Dev

Why is a file with 400 permissions seen writable by root but read-only by user?

분류에서Dev

Evince Document Viewer - Failed to execute child process "nemo" (Permission denied)

분류에서Dev

Failed to spawn bluetooth main process: unable to execute: permission denied

분류에서Dev

How to grant Linux read permission for a user to a particular file owned by root?

분류에서Dev

Why I can't erase a hidden file with root privileges?

분류에서Dev

Not root user does not have access on Mounted SSD

분류에서Dev

Allow user other than root to restart supervisorctl process?

분류에서Dev

Why crontab doesn't want to execute the job as root?

분류에서Dev

Why does Ubuntu have a disabled root account?

분류에서Dev

Why does NVL2 returns NULL when it's not expected?

분류에서Dev

pushing messages as root to user using startx

분류에서Dev

maximum sum of value from root to leaf in a binary tree using stack

분류에서Dev

Access denied for user 'root'@'localhost' with certain passwords only

분류에서Dev

ld: fatal: relocations remain against allocatable but non-writable sections, when compiling libssh2 on solaris

분류에서Dev

cannot execute binary file using execlp() with command in a string

분류에서Dev

When creating a stored procedure in oracle, it appears to execute, but it does nothing. The stored procedure never gets saved. Why?

분류에서Dev

Why does my C++ function, only when it's placed after main(), not work?

분류에서Dev

Why does laptop's battery drain only when I am playing Minecraft?

분류에서Dev

Why does my 1440p HDMI monitor only display correctly when running from the bootable USB?

분류에서Dev

Why does my system show only 3.2 GiB of RAM when I definitely have 4.0 GiB

분류에서Dev

Why am I getting "Permission denied: make_sock: could not bind to address" when starting Apache2?

분류에서Dev

No permission to create sub directory when user belongs to directory group

Related 관련 기사

  1. 1

    Why does (ps -f) create no subshell but a separate process?

  2. 2

    Why a folder needs a execute permission when I'm trying to execute a .sh?

  3. 3

    Binary Search Tree Traversals failed to execute when using recursion

  4. 4

    Why does initramfs mount the root filesystem read-only

  5. 5

    Why does Tk().after only execute once and how to fix it?

  6. 6

    process Count of each user in ps aux command

  7. 7

    Why does ssh look for keys in /root/.ssh when run with sudo?

  8. 8

    Why is a file with 400 permissions seen writable by root but read-only by user?

  9. 9

    Evince Document Viewer - Failed to execute child process "nemo" (Permission denied)

  10. 10

    Failed to spawn bluetooth main process: unable to execute: permission denied

  11. 11

    How to grant Linux read permission for a user to a particular file owned by root?

  12. 12

    Why I can't erase a hidden file with root privileges?

  13. 13

    Not root user does not have access on Mounted SSD

  14. 14

    Allow user other than root to restart supervisorctl process?

  15. 15

    Why crontab doesn't want to execute the job as root?

  16. 16

    Why does Ubuntu have a disabled root account?

  17. 17

    Why does NVL2 returns NULL when it's not expected?

  18. 18

    pushing messages as root to user using startx

  19. 19

    maximum sum of value from root to leaf in a binary tree using stack

  20. 20

    Access denied for user 'root'@'localhost' with certain passwords only

  21. 21

    ld: fatal: relocations remain against allocatable but non-writable sections, when compiling libssh2 on solaris

  22. 22

    cannot execute binary file using execlp() with command in a string

  23. 23

    When creating a stored procedure in oracle, it appears to execute, but it does nothing. The stored procedure never gets saved. Why?

  24. 24

    Why does my C++ function, only when it's placed after main(), not work?

  25. 25

    Why does laptop's battery drain only when I am playing Minecraft?

  26. 26

    Why does my 1440p HDMI monitor only display correctly when running from the bootable USB?

  27. 27

    Why does my system show only 3.2 GiB of RAM when I definitely have 4.0 GiB

  28. 28

    Why am I getting "Permission denied: make_sock: could not bind to address" when starting Apache2?

  29. 29

    No permission to create sub directory when user belongs to directory group

뜨겁다태그

보관