Subject: linux-next: manual merge of the security tree with the vfs-brauner tree

From: Stephen Rothwell <[email-masked]>
Date: Tue, 10 Sep 2024 13:27:40 +1000

Hi all,

Today's linux-next merge of the security tree got a conflict in:

  fs/fcntl.c

between commit:

  1934b212615d ("file: reclaim 24 bytes from f_owner")

from the vfs-brauner tree and commit:

  26f204380a3c ("fs: Fix file_set_fowner LSM hook inconsistencies")

from the security tree.

I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging.  You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.

-- 
Cheers,
Stephen Rothwell

diff --cc fs/fcntl.c
index f6fde75a3bd5,c28dc6c005f1..000000000000
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@@ -89,72 -87,24 +89,66 @@@ static int setfl(int fd, struct file * 
  	return error;
  }
  
 +/*
 + * Allocate an file->f_owner struct if it doesn't exist, handling racing
 + * allocations correctly.
 + */
 +int file_f_owner_allocate(struct file *file)
 +{
 +	struct fown_struct *f_owner;
 +
 +	f_owner = file_f_owner(file);
 +	if (f_owner)
 +		return 0;
 +
 +	f_owner = kzalloc(sizeof(struct fown_struct), GFP_KERNEL);
 +	if (!f_owner)
 +		return -ENOMEM;
 +
 +	rwlock_init(&f_owner->lock);
 +	f_owner->file = file;
 +	/* If someone else raced us, drop our allocation. */
 +	if (unlikely(cmpxchg(&file->f_owner, NULL, f_owner)))
 +		kfree(f_owner);
 +	return 0;
 +}
 +EXPORT_SYMBOL(file_f_owner_allocate);
 +
 +void file_f_owner_release(struct file *file)
 +{
 +	struct fown_struct *f_owner;
 +
 +	f_owner = file_f_owner(file);
 +	if (f_owner) {
 +		put_pid(f_owner->pid);
 +		kfree(f_owner);
 +	}
 +}
 +
- 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) {
 -		put_pid(filp->f_owner.pid);
 -		filp->f_owner.pid = get_pid(pid);
 -		filp->f_owner.pid_type = type;
 +	struct fown_struct *f_owner;
 +
 +	f_owner = file_f_owner(filp);
 +	if (WARN_ON_ONCE(!f_owner))
 +		return;
 +
 +	write_lock_irq(&f_owner->lock);
 +	if (force || !f_owner->pid) {
 +		put_pid(f_owner->pid);
 +		f_owner->pid = get_pid(pid);
 +		f_owner->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;
 +			f_owner->uid = cred->uid;
 +			f_owner->euid = cred->euid;
  		}
  	}
 -	write_unlock_irq(&filp->f_owner.lock);
 +	write_unlock_irq(&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)