From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-bc0f.mail.infomaniak.ch (smtp-bc0f.mail.infomaniak.ch [45.157.188.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F131F16D332 for ; Sun, 8 Sep 2024 18:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725820216; cv=none; b=hWM4jAUNNsYFJ8WxVzkLAmBeVjgvoMBjnlH1vtfaiLJOhGhE3vk9g9xV1gmlAKXh35NMeLTIs3+3z1sEEr2gwK7fO9OpOu/YZMsVjWaIQM15BqvXw2znasvzs/xpXQGMabQvg9eCUkJVNWTg+50ZkcVy1qH1G6xPkA3aXIWunf0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725820216; c=relaxed/simple; bh=gNKnIuGvVMJr191hD6VCownoEv1f54m/1RShTokZqUM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oJYljIFJyBpvrG1yLLWAeQFY26uAo8QYYfrb7PLmgESW5DzdZQjwAlMhQn8a/iW8XmJCy8khTWF6hkFGgzmmZLlzgLfpg6tJ1EHsUS1WLUfZgAzeuIYj+2E17eAO0PQPa4CTadCU/6kBwNlNTL/SL5NANOUcjODQsIe7wmgYMHs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b=F2aybbR9; arc=none smtp.client-ip=45.157.188.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="F2aybbR9" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4X1yhM005Xzn4P; Sun, 8 Sep 2024 20:11:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1725819074; bh=nXI9xic4x3kUhmU/n/oTnixYiW2o/Ba+mWZgJ65FcYU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=F2aybbR9SGRO8ntewsvTYFuHGvAUHrhTiLBB/jRK1/tkv4vPCc+/z+dcy1Fzwh98K h8Ldft3UX4SPDXPe0qt22j+0PNrLPVgH4fvDqcSjzyuzHoDQmAVbGsLM9CpwGZ7gy7 loxmwtN9/Hw7IBhxZB2PN6qlA9udH6kKLaD+ydOU= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4X1yhK3qYJzggS; Sun, 8 Sep 2024 20:11:13 +0200 (CEST) Date: Sun, 8 Sep 2024 20:11:07 +0200 From: =?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?= To: Paul Moore Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, Jan Kara , Tahera Fahimi , Mateusz Guzik , Al Viro , Casey Schaufler , James Morris , Jann Horn , Ondrej Mosnacek , "Serge E. Hallyn" , Stephen Smalley Subject: Re: [PATCH v3 1/2] fs: Fix file_set_fowner LSM hook inconsistencies Message-ID: <20240908.jeim4Aif3Fee@digikod.net> References: <20240821095609.365176-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Infomaniak-Routing: alpha On Wed, Aug 21, 2024 at 12:32:17PM -0400, Paul Moore wrote: > On Wed, Aug 21, 2024 at 5:56 AM Mickaël Salaün wrote: > > > > The fcntl's F_SETOWN command sets the process that handle SIGIO/SIGURG > > for the related file descriptor. Before this change, the > > file_set_fowner LSM hook was always called, ignoring the VFS logic which > > may not actually change the process that handles SIGIO (e.g. TUN, TTY, > > dnotify), nor update the related UID/EUID. > > > > Moreover, because security_file_set_fowner() was called without lock > > (e.g. f_owner.lock), concurrent F_SETOWN commands could result to a race > > condition and inconsistent LSM states (e.g. SELinux's fown_sid) compared > > to struct fown_struct's UID/EUID. > > > > This change makes sure the LSM states are always in sync with the VFS > > state by moving the security_file_set_fowner() call close to the > > UID/EUID updates and using the same f_owner.lock . > > > > Rename f_modown() to __f_setown() to simplify code. > > > > Cc: Al Viro > > Cc: Casey Schaufler > > Cc: Christian Brauner > > Cc: James Morris > > Cc: Jann Horn > > Cc: Ondrej Mosnacek > > Cc: Paul Moore > > Cc: Serge E. Hallyn > > Cc: Stephen Smalley > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > > Signed-off-by: Mickaël Salaün > > --- > > > > Changes since v2: > > https://lore.kernel.org/r/20240812174421.1636724-1-mic@digikod.net > > - Only keep the LSM hook move. > > > > Changes since v1: > > https://lore.kernel.org/r/20240812144936.1616628-1-mic@digikod.net > > - Add back the file_set_fowner hook (but without user) as > > requested by Paul, but move it for consistency. > > --- > > fs/fcntl.c | 14 ++++---------- > > 1 file changed, 4 insertions(+), 10 deletions(-) > > This looks reasonable to me, and fixes a potential problem with > existing LSMs. Unless I hear any strong objections I'll plan to merge > this, and patch 2/2, into the LSM tree tomorrow. I didn't see these patches in -next, did I miss something? Landlock will use this hook really soon and it would make it much easier if these patches where upstream before. > > > diff --git a/fs/fcntl.c b/fs/fcntl.c > > index 300e5d9ad913..c28dc6c005f1 100644 > > --- a/fs/fcntl.c > > +++ b/fs/fcntl.c > > @@ -87,8 +87,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg) > > return error; > > } > > > > -static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, > > - int force) > > +void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, > > + int force) > > { > > write_lock_irq(&filp->f_owner.lock); > > if (force || !filp->f_owner.pid) { > > @@ -98,19 +98,13 @@ static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, > > > > if (pid) { > > const struct cred *cred = current_cred(); > > + security_file_set_fowner(filp); > > filp->f_owner.uid = cred->uid; > > filp->f_owner.euid = cred->euid; > > } > > } > > write_unlock_irq(&filp->f_owner.lock); > > } > > - > > -void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, > > - int force) > > -{ > > - security_file_set_fowner(filp); > > - f_modown(filp, pid, type, force); > > -} > > EXPORT_SYMBOL(__f_setown); > > > > int f_setown(struct file *filp, int who, int force) > > @@ -146,7 +140,7 @@ EXPORT_SYMBOL(f_setown); > > > > void f_delown(struct file *filp) > > { > > - f_modown(filp, NULL, PIDTYPE_TGID, 1); > > + __f_setown(filp, NULL, PIDTYPE_TGID, 1); > > } > > > > pid_t f_getown(struct file *filp) > > -- > > 2.46.0 > > -- > paul-moore.com >