From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753285AbcLLTNk (ORCPT ); Mon, 12 Dec 2016 14:13:40 -0500 Received: from out02.mta.xmission.com ([166.70.13.232]:41959 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752096AbcLLTNj (ORCPT ); Mon, 12 Dec 2016 14:13:39 -0500 From: ebiederm@xmission.com (Eric W. Biederman) To: Oleg Nesterov Cc: EunTaik Lee , "mingo\@redhat.com" , "peterz\@infradead.org" , "linux-kernel\@vger.kernel.org" References: <20161209093351epcms1p418673c3cdec7d4c3e81b5df131173c57@epcms1p4> <20161209172114.GA25742@redhat.com> <87wpf8hhfc.fsf@xmission.com> <20161212134611.GA11078@redhat.com> Date: Tue, 13 Dec 2016 08:10:26 +1300 In-Reply-To: <20161212134611.GA11078@redhat.com> (Oleg Nesterov's message of "Mon, 12 Dec 2016 14:46:11 +0100") Message-ID: <87bmwhdkv1.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=1cGW2V-000429-9B;;;mid=<87bmwhdkv1.fsf@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=101.100.131.98;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/RppdacSWVaS5s9JhcqflBzkhD/upNLVA= X-SA-Exim-Connect-IP: 101.100.131.98 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 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 * [sa06 1397; Body=1 Fuz1=1 Fuz2=1] * 1.0 T_XMDrugObfuBody_12 obfuscated drug references * 0.0 T_TooManySym_02 5+ unique symbols in subject * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: XMission; sa06 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Oleg Nesterov X-Spam-Relay-Country: X-Spam-Timing: total 335 ms - load_scoreonly_sql: 0.03 (0.0%), signal_user_changed: 7 (2.2%), b_tie_ro: 6 (1.9%), parse: 0.77 (0.2%), extract_message_metadata: 3.2 (1.0%), get_uri_detail_list: 1.70 (0.5%), tests_pri_-1000: 3.6 (1.1%), tests_pri_-950: 1.16 (0.3%), tests_pri_-900: 0.96 (0.3%), tests_pri_-400: 31 (9.3%), check_bayes: 30 (8.9%), b_tokenize: 12 (3.6%), b_tok_get_all: 8 (2.4%), b_comp_prob: 3.7 (1.1%), b_tok_touch_all: 3.1 (0.9%), b_finish: 0.68 (0.2%), tests_pri_0: 275 (82.1%), check_dkim_signature: 0.79 (0.2%), check_dkim_adsp: 2.9 (0.9%), tests_pri_500: 3.9 (1.2%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH] sched/pid fix use-after free in task_tgid_vnr 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 Oleg Nesterov writes: > On 12/10, Eric W. Biederman wrote: >> >> Oleg Nesterov writes: >> >> > On 12/09, EunTaik Lee wrote: >> >> >> >> There is a use-after-free case with below call stack. >> >> >> >> pid_nr_ns+0x10/0x38 >> >> cgroup_pidlist_start+0x144/0x400 >> >> cgroup_seqfile_start+0x1c/0x24 >> >> kernfs_seq_start+0x54/0x90 >> >> seq_read+0x15c/0x3a8 >> >> kernfs_fop_read+0x38/0x160 >> >> __vfs_read+0x28/0xc8 >> >> vfs_read+0x84/0xfc >> >> How is this a use after free. The function pid_nr_ns should take a NULL pointer >> as input and return 0? > > No, the task (task_struct) itself can't go away, but task->group_leader > can point to nowhere. > >> Certainly if the addtion of pid_alive fixes it pid_vnr(task_tgid(tsk)) >> is fine. Are we perhaps missing rcu locking? > > rcu_read_lock() is not enough in this case, see below. > >> Or is the problem simply that in task_tgid we are accessing >> task->group_leader which may already be dead? > > Yes. Lets forget about the callchain above, I didn't even bother to verify > that it can actually hit the problem. Although I think EunTaik is very right, > css_task_iter_next() does get_task_struct() and drops css_set_lock, the task > can exit after that. Forget. > > Just suppose that a task simply does > > pid = task_tgid_vnr(current); > > after it has already called exit_notify(). And this is what perf_event_pid() > does, perhaps we have more buggy users. > > In this case current->group_leader or parent/real_parent can point to the > exited/freed tasks. I already said this many times, ee really need to nullify > them in __unhash_process() but this needs a lot of (mostly simple) > cleanups. Is there anything wrong with starting with the patch below? diff --git a/kernel/exit.c b/kernel/exit.c index 9d68c45ebbe3..03daeecc335d 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -200,6 +200,7 @@ void release_task(struct task_struct *p) if (zap_leader) leader->exit_state = EXIT_DEAD; } + p->group_leader = NULL; write_unlock_irq(&tasklist_lock); release_thread(p); That seems to cut to the heart of the matter. Failures will be clearer, as will be code that is introduced to handle the situation. Then we don't need pid_alive or any other magic just a simple: rcu_read_lock(); leader = READ_ONCE(task->group_leader); if (leader) { /* Do stuff */ } rcu_read_unlock(); >> If so the fix needs to be >> in task_tgid. > > Yes, task_tgid() should probably return NULL in this case, but this connects > to "a lot of cleanups" above. But that is important because that is where things go wrong in the specific case under discussion. pid_nr_ns handles all of the other cases, it is task_tgid that went wrong. Eric