This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [patch] Fix fileio.exp failuew when run as root user.
- From: Yao Qi <yao at codesourcery dot com>
- To: pmuldoon at redhat dot com
- Cc: gdb-patches at sourceware dot org
- Date: Tue, 14 Dec 2010 17:08:23 +0800
- Subject: Re: [patch] Fix fileio.exp failuew when run as root user.
- References: <m3bp5ll1c5.fsf@redhat.com>
On 11/20/2010 12:34 AM, Phil Muldoon wrote:
>
> This patch has been carried in the Fedora RPM since 6.8. We are
> submitting it for inclusion upstream.
>
> gdb.base/fileio.exp|c fail when this test is executed as a root. This
> test fixes that.
I don't have Fedora system on hand, but I can reproduce this problem on
Ubuntu with 'sudo make check RUNTESTFLAGS="fileio.exp"', and get
following two fails.
FAIL: gdb.base/fileio.exp: Open for write but no write permission
returns EACCES
FAIL: gdb.base/fileio.exp: Unlinking a file in a directory w/o write
access returns EACCES
If they are what you want to fix in this patch, here is one cent,
> int
> main ()
> {
> + /* ROOTSUBDIR is already prepared by fileio.exp. We use it for easy cleanup
> + (by fileio.exp) if we are run by multiple users in the same directory. */
> +
> + if (chdir (ROOTSUBDIR) != 0)
> + {
> + printf ("chdir " ROOTSUBDIR ": %s\n", strerror (errno));
> + exit (1);
> + }
> +
> + /* These tests
> + Open for write but no write permission returns EACCES
> + Unlinking a file in a directory w/o write access returns EACCES
> + fail if we are being run as root - drop the privileges here. */
> +
> + if (geteuid () == 0)
> + {
> + uid_t uid = 99;
> +
> + if (chown (".", uid, uid) != 0)
> + {
> + printf ("chown %d.%d " ROOTSUBDIR ": %s\n", (int) uid, (int) uid,
> + strerror (errno));
> + exit (1);
> + }
> + if (setuid (uid) || geteuid () == 0)
> + {
> + printf ("setuid %d: %s\n", (int) uid, strerror (errno));
> + exit (1);
> + }
> + }
> +
We can extract these code into a separate function, and replace
"exit(1)" by "return 1".
/* Try to drop root privileges of this process. Return 0 if it is done
successfully, otherwise return non-zero. */
int try_drop_root_privilege()
{
....
}
int
main ()
{
if (try_drop_root_privilege())
{
exit (1);
}
...
...
}
--
Yao (éå)