From mboxrd@z Thu Jan 1 00:00:00 1970 From: Al Viro Subject: Re: [Patch net] socket: close race condition between sock_close() and sockfs_setattr() Date: Thu, 7 Jun 2018 23:32:43 +0100 Message-ID: <20180607223242.GY30522@ZenIV.linux.org.uk> References: <20180607203949.16945-1-xiyou.wangcong@gmail.com> <20180607212631.GW30522@ZenIV.linux.org.uk> <20180607220446.GX30522@ZenIV.linux.org.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Linux Kernel Network Developers , shankarapailoor , Tetsuo Handa , Lorenzo Colitti To: Cong Wang Return-path: Received: from zeniv.linux.org.uk ([195.92.253.2]:57310 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751938AbeFGWcp (ORCPT ); Thu, 7 Jun 2018 18:32:45 -0400 Content-Disposition: inline In-Reply-To: Sender: netdev-owner@vger.kernel.org List-ID: On Thu, Jun 07, 2018 at 03:15:15PM -0700, Cong Wang wrote: > > You do realize that the same ->setattr() can be called by way of > > chown() on /proc/self/fd/, right? What would you do there - > > bump refcount on that struct file when traversing that symlink and > > hold it past the end of pathname resolution, until... what exactly? > > I was thinking about this: > > error = user_path_at(dfd,....); // hold dfd when needed > > if (!error) { > error = chown_common(&path, mode); > path_put(&path); // release dfd if held > > With this, we can guarantee ->release() is only possibly called > after chown_common() which is after ->setattr() too. No, we can't. You are assuming that there *is* dfd and that it points to the opened socket we are going to operate upon. That is not guaranteed. At all. If e.g. 42 is a file descriptor of an opened socket, plain chown(2) on /proc/self/fd/42 will trigger that ->setattr(). No dfd in sight. We do run across an opened file at some point, all right - when we traverse the symlink in procfs. You can't bump ->f_count there. Even leaving aside the "where would I stash the reference to that file?" and "how long would I hold it?", you can't bump ->f_count on other process' files. That would bugger the expectations of close(2) callers.