From: Andy Isaacson <adi@hexapodia.org>
To: linux-kernel@vger.kernel.org
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Subject: setreuid() results in unreadable /proc/self/fdinfo/
Date: Wed, 23 May 2012 13:58:57 -0700 [thread overview]
Message-ID: <20120523205857.GA22643@hexapodia.org> (raw)
The enclosed testcase shows that after using setreuid to permanently
give up root privs, the process loses its ability to open
/proc/self/fdinfo (as well as some but not all other entries in
/proc/self/).
This seems to fail only with threads -- a singlethreaded program does
not show the same failure. The failure is the same if the setreuid is
done in the parent thread (before pthread_create) or in the child
thread.
This testcase shows the same behavior on RHEL5 and on
3.4.0-rc4-00095-g95f7147.
This was originally found in Java code using the jsvc project.
A similar discussion happened 3.5 years ago (!) in
http://lkml.indiana.edu/hypermail/linux/kernel/0808.0/3350.html
(CCing Alexey.)
% cc -O2 -Wall setuid-proc-self-fd.c -o setuid-proc-self-fd -lpthread
% sudo ./setuid-proc-self-fd
uid = 0 euid = 0
uid = 1000 euid = 1000
main created thread, waiting.
/proc/self/fdinfo: Permission denied
delaying 100 seconds.
...
% sudo ls -ld /proc/`pidof setuid-proc-self-fd`{,/task/*}{,/fdinfo}
dr-xr-xr-x 7 andy root 0 May 23 13:43 /proc/31640
dr-x------ 2 root root 0 May 23 13:43 /proc/31640/fdinfo
dr-xr-xr-x 5 andy root 0 May 23 13:44 /proc/31640/task/31640
dr-x------ 2 root root 0 May 23 13:44 /proc/31640/task/31640/fdinfo
dr-xr-xr-x 5 andy root 0 May 23 13:44 /proc/31640/task/31641
dr-x------ 2 root root 0 May 23 13:44 /proc/31640/task/31641/fdinfo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
void die(char *fmt, ...)
__attribute__((noreturn))
__attribute__((format(printf, 1, 2)));
void die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
void *do_child(void *arg)
{
int fd;
if((fd = open("/proc/self/fdinfo", O_RDONLY|O_DIRECTORY)) == -1) {
fprintf(stderr, "/proc/self/fdinfo: %s\n", strerror(errno));
fprintf(stderr, "delaying 100 seconds.\n");
sleep(100);
}
printf("fd = %d\n", fd);
fflush(stdout);
return 0;
}
int main(int argc, char **argv)
{
pthread_t t;
printf("uid = %d euid = %d\n", (int)getuid(), (int)geteuid());
setreuid(1000,1000);
printf("uid = %d euid = %d\n", (int)getuid(), (int)geteuid());
pthread_create(&t, 0, do_child, 0);
printf("main created thread, waiting.\n");
pthread_join(t, 0);
printf("main exiting.\n");
return 0;
}
-andy
next reply other threads:[~2012-05-23 21:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-23 20:58 Andy Isaacson [this message]
2012-05-24 18:41 ` Andy Isaacson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120523205857.GA22643@hexapodia.org \
--to=adi@hexapodia.org \
--cc=adobriyan@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome