From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2915 invoked by alias); 14 Dec 2010 09:08:38 -0000 Received: (qmail 2903 invoked by uid 22791); 14 Dec 2010 09:08:37 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail.codesourcery.com (HELO mail.codesourcery.com) (38.113.113.100) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 14 Dec 2010 09:08:32 +0000 Received: (qmail 4853 invoked from network); 14 Dec 2010 09:08:30 -0000 Received: from unknown (HELO ?192.168.0.102?) (yao@127.0.0.2) by mail.codesourcery.com with ESMTPA; 14 Dec 2010 09:08:30 -0000 Message-ID: <4D073407.7070602@codesourcery.com> Date: Tue, 14 Dec 2010 09:08:00 -0000 From: Yao Qi User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 MIME-Version: 1.0 To: pmuldoon@redhat.com CC: gdb-patches@sourceware.org Subject: Re: [patch] Fix fileio.exp failuew when run as root user. References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2010-12/txt/msg00218.txt.bz2 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 (齐尧)