From ba89f9ebf449db752d535d4a66ad8c39981eef86 Mon Sep 17 00:00:00 2001 From: Yura Pakhuchiy Date: Wed, 12 Sep 2007 15:55:37 +0300 Subject: [PATCH] More sane set-uid-root handling --- ntfsprogs/ntfsmount.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ntfsprogs/ntfsmount.c b/ntfsprogs/ntfsmount.c index f0ebc0ec..5819af57 100644 --- a/ntfsprogs/ntfsmount.c +++ b/ntfsprogs/ntfsmount.c @@ -1734,13 +1734,22 @@ int main(int argc, char *argv[]) ntfs_fuse_destroy(NULL); return 1; } - /* Gain root privileges if required. */ - if (ctx->blkdev) + if (ctx->blkdev) { + /* Gain root privileges for blkdev mount. */ if (setuid(0)) { ntfs_log_perror("setuid(0) failed"); fuse_opt_free_args(&args); ntfs_fuse_destroy(NULL); + return 1; } + } else { + /* + * Drop effective uid if our binary is set-uid-root and we are + * performing not blkdev mount. + */ + if (!geteuid() && seteuid(ctx->uid)) + ntfs_log_perror("Failed to drop effective uid"); + } /* Create filesystem (FUSE part). */ fch = fuse_mount(ctx->mnt_point, &args); if (!fch) { @@ -1758,8 +1767,8 @@ int main(int argc, char *argv[]) ntfs_fuse_destroy(NULL); return 1; } - /* Drop root privileges. */ - if (setuid(ctx->uid) || seteuid(ctx->uid)) + /* Drop root privileges if we obtained them. */ + if (ctx->blkdev && (setuid(ctx->uid) || seteuid(ctx->uid))) ntfs_log_warning("Failed to drop root privileges.\n"); /* Detach from terminal. */ if (!ctx->debug && !ctx->no_detach) {