From bc0dc00da7cf99863b5581aa6dd659370a9ef6e4 Mon Sep 17 00:00:00 2001 From: attila Date: Mon, 8 Dec 2003 11:21:59 +0000 Subject: Security fixes for fibmap_mplayer by Adam Rice git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@11579 b3059339-0415-0410-9bf9-f77b7e298cf2 --- fibmap_mplayer.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'fibmap_mplayer.c') diff --git a/fibmap_mplayer.c b/fibmap_mplayer.c index 75395a90a8..897a4d1605 100644 --- a/fibmap_mplayer.c +++ b/fibmap_mplayer.c @@ -11,28 +11,51 @@ #include #include +#include "mp_msg.h" + #ifndef FIBMAP #define FIBMAP 1 #endif int main ( int argc , char ** argv ) { - int fd,lba=0; - if (argc!=2) { - fprintf(stderr,"Bad usage.\n"); + int fd,ret,lba=0; + if (geteuid()!=0) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL, "%s must be setuid root to work\n", + argv[0]); + return 1; + } + if (seteuid(getuid()) == -1) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL, "Couldn't drop privileges: %s\n", + strerror(errno)); + return 1; + } + if (argc!=2 || argv[1]==NULL) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL,"Usage: %s \n", argv[0]); return 1; } if ((fd = open(argv[1], O_RDONLY)) == -1) { - fprintf(stderr,"Cannot open file %s: %s\n", - argv[1] ? argv[1] : "(NULL)", strerror(errno)); + mp_msg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open file %s: %s\n", + argv[1], strerror(errno)); return 1; } - if (ioctl(fd, FIBMAP, &lba) != 0) { - fprintf(stderr,"fibmap ioctl: %s (Hint: %s is not suid root?)\n",strerror(errno),argv[0]); - close(fd); + if (seteuid(0) == -1) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL, "Couldn't restore root privileges: %s\n", + strerror(errno)); + return 1; + } + ret = ioctl(fd, FIBMAP, &lba); + if (seteuid(getuid()) == -1) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL, "Couldn't re-drop privileges: %s\n", + strerror(errno)); + return 1; + } + close(fd); + if (ret != 0) { + mp_msg(MSGT_CPLAYER,MSGL_FATAL,"fibmap ioctl failed: %s\n", + strerror(errno)); return 1; } - close(fd); printf("%d\n",lba); return 0; } -- cgit v1.2.3