From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752418AbdGFMqG (ORCPT ); Thu, 6 Jul 2017 08:46:06 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:57118 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751994AbdGFMqD (ORCPT ); Thu, 6 Jul 2017 08:46:03 -0400 From: ebiederm@xmission.com (Eric W. Biederman) To: Kees Cook Cc: Linus Torvalds , Andy Lutomirski , Michal Hocko , Ben Hutchings , Willy Tarreau , Hugh Dickins , Oleg Nesterov , "Jason A. Donenfeld" , Rik van Riel , Larry Woodman , "Kirill A. Shutemov" , Tony Luck , "James E.J. Bottomley" , Helge Diller , James Hogan , Laura Abbott , Greg KH , "security\@kernel.org" , Qualys Security Advisory , LKML , Ximin Luo References: <20170706043235.GA36170@beast> Date: Thu, 06 Jul 2017 07:38:06 -0500 In-Reply-To: <20170706043235.GA36170@beast> (Kees Cook's message of "Wed, 5 Jul 2017 21:32:35 -0700") Message-ID: <87wp7l68r5.fsf@xmission.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1dT6AN-0005Ny-3o;;;mid=<87wp7l68r5.fsf@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=67.3.213.87;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX18TFbxsbNkiGkLv57PDKSUgTDwSbkIr0Bk= X-SA-Exim-Connect-IP: 67.3.213.87 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.0 TVD_RCVD_IP Message was received from an IP address * 1.5 TR_Symld_Words too many words that have symbols inside * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa02 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa02 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;Kees Cook X-Spam-Relay-Country: X-Spam-Timing: total 5347 ms - load_scoreonly_sql: 0.17 (0.0%), signal_user_changed: 4.2 (0.1%), b_tie_ro: 2.5 (0.0%), parse: 6 (0.1%), extract_message_metadata: 35 (0.7%), get_uri_detail_list: 5 (0.1%), tests_pri_-1000: 17 (0.3%), tests_pri_-950: 2.5 (0.0%), tests_pri_-900: 2.0 (0.0%), tests_pri_-400: 58 (1.1%), check_bayes: 56 (1.0%), b_tokenize: 23 (0.4%), b_tok_get_all: 13 (0.2%), b_comp_prob: 7 (0.1%), b_tok_touch_all: 7 (0.1%), b_finish: 1.17 (0.0%), tests_pri_0: 890 (16.6%), check_dkim_signature: 1.45 (0.0%), check_dkim_adsp: 6 (0.1%), tests_pri_500: 4322 (80.8%), poll_dns_idle: 4300 (80.4%), rewrite_mail: 0.00 (0.0%) Subject: Re: [RFC][PATCH] exec: Use init rlimits for setuid exec X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kees Cook writes: > In an attempt to provide sensible rlimit defaults for setuid execs, this > inherits the namespace's init rlimits: > > $ ulimit -s > 8192 > $ ulimit -s unlimited > $ /bin/sh -c 'ulimit -s' > unlimited > $ sudo /bin/sh -c 'ulimit -s' > 8192 > > This is modified from Brad Spengler/PaX Team's hard-coded setuid exec > stack rlimit (8MB) in the last public patch of grsecurity/PaX based on > my understanding of the code. Changes or omissions from the original > code are mine and don't reflect the original grsecurity/PaX code. > > Signed-off-by: Kees Cook > --- > Instead of copying all rlimits, we could also pick specific ones to copy > (e.g. RLIMIT_STACK, or ones from Andy's list) or exclude from copying > (probably better to blacklist than whitelist). > > I think this is the right way to find the ns init task, but maybe it > needs locking? > --- > fs/exec.c | 34 ++++++++++++++++++++++++++++++---- > 1 file changed, 30 insertions(+), 4 deletions(-) > > diff --git a/fs/exec.c b/fs/exec.c > index 904199086490..80e8b2bd4284 100644 > --- a/fs/exec.c > +++ b/fs/exec.c > @@ -1675,6 +1675,12 @@ static int exec_binprm(struct linux_binprm *bprm) > return ret; > } > > +static inline bool is_setuid_exec(struct linux_binprm *bprm) > +{ > + return (!uid_eq(bprm->cred->euid, current_euid()) || > + !gid_eq(bprm->cred->egid, current_egid())); > +} Awesome I can make an executable setuid to myself and get all of roots rlimits! Scratch inheritable rlimits as useful for any kind of policy decision. > /* > * sys_execve() executes a new program. > */ > @@ -1687,6 +1693,7 @@ static int do_execveat_common(int fd, struct filename *filename, > struct linux_binprm *bprm; > struct file *file; > struct files_struct *displaced; > + struct rlimit saved_rlim[RLIM_NLIMITS]; > int retval; > > if (IS_ERR(filename)) > @@ -1771,24 +1778,38 @@ static int do_execveat_common(int fd, struct filename *filename, > if (retval < 0) > goto out; > > + /* > + * From here forward, we've got credentials set up and we're > + * using resources, so do rlimit replacement before we start > + * copying strings. (Note that the RLIMIT_NPROC check has > + * already happened.) > + */ > + BUILD_BUG_ON(sizeof(saved_rlim) != sizeof(current->signal->rlim)); > + if (is_setuid_exec(bprm)) { > + memcpy(saved_rlim, current->signal->rlim, sizeof(saved_rlim)); > + memcpy(current->signal->rlim, > + task_active_pid_ns(current)->child_reaper->signal->rlim, > + sizeof(current->signal->rlim)); > + } > + Caerful. child_reaper can change if you are not holding the tasklist lock. It would be better if we could move any rlimit changes after de_thread. Otherwise there are some really fun races you can play with. After de_thread is past the point of no return so you would not need to worry about restoring the rlimits either. > retval = copy_strings_kernel(1, &bprm->filename, bprm); > if (retval < 0) > - goto out; > + goto out_restore; > > bprm->exec = bprm->p; > retval = copy_strings(bprm->envc, envp, bprm); > if (retval < 0) > - goto out; > + goto out_restore; > > retval = copy_strings(bprm->argc, argv, bprm); > if (retval < 0) > - goto out; > + goto out_restore; > > would_dump(bprm, bprm->file); > > retval = exec_binprm(bprm); > if (retval < 0) > - goto out; > + goto out_restore; > > /* execve succeeded */ > current->fs->in_exec = 0; > @@ -1802,6 +1823,11 @@ static int do_execveat_common(int fd, struct filename *filename, > put_files_struct(displaced); > return retval; > > +out_restore: > + if (is_setuid_exec(bprm)) { > + memcpy(current->signal->rlim, saved_rlim, sizeof(saved_rlim)); > + } > + > out: > if (bprm->mm) { > acct_arg_size(bprm, 0); > -- > 2.7.4 Eric