Skip to contentSkip to conversation

problems with alloc_file_pseudo() use in __nfs42_ssc_open()

RFC Al Viro 5 participants 14 messages Aug 17, 2022 – Aug 19, 2022

Subsystems (3)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

2) if alloc_file_pseudo() fails there, we get an inode leak. It needs an iput() for that case. As in

        if (IS_ERR(filep)) {
		res = ERR_CAST(filep);
		iput(r_ino);
		goto out_free_name;
	}

But I'd like to point out that alloc_file_pseudo() is not inteded for use on normal filesystem's inodes - the use here *mostly* works (directories aside), but... Use it on filesystem with non-trivial default dentry_operations and things will get interesting, etc.

#1 lore raw
Quoted 5 lines · Al Viro

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

Quoted 7 lines

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

This inode is created to make an appearance of an opened file to do (an NFS) read, it's never a directory.

Quoted 12 lines

2) if alloc_file_pseudo() fails there, we get an inode leak. It needs an iput() for that case. As in

        if (IS_ERR(filep)) {
                res = ERR_CAST(filep);
                iput(r_ino);
                goto out_free_name;
        }

But I'd like to point out that alloc_file_pseudo() is not inteded for use on normal filesystem's inodes - the use here *mostly* works (directories aside), but... Use it on filesystem with non-trivial default dentry_operations and things will get interesting, etc.

author maintainer
#2 lore raw
Quoted 14 lines · Olga Kornievskaia
Quoted 5 lines · Al Viro

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

Quoted 7 lines

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

Quoted 2 lines

This inode is created to make an appearance of an opened file to do (an NFS) read, it's never a directory.

Er... Where does the fhandle come from? From my reading it's a client-sent data; I don't know what trust model do you assume, but the price of getting multiple dentries over the same directory inode is high. Bogus or compromised client should not be able to cause severe corruption of kernel data structures...

#3 lore raw
Quoted 16 lines · Al Viro
Quoted 14 lines · Olga Kornievskaia
Quoted 12 lines · Al Viro

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

Quoted 8 lines
Quoted 2 lines

This inode is created to make an appearance of an opened file to do (an NFS) read, it's never a directory.

Er... Where does the fhandle come from? From my reading it's a client-sent data; I don't know what trust model do you assume, but the price of getting multiple dentries over the same directory inode is high. Bogus or compromised client should not be able to cause severe corruption of kernel data structures...

This is an NFS spec specified operation. The (source file's) filehandle comes from the COPY operation compound that the destination server gets and then uses -- creates an inode from using the code you are looking at now -- to access from the source server. Security is all described in the spec. The uniqueness of the filehandle is provided by the source server that created it.

author maintainer
#4 lore raw
Quoted 26 lines · Olga Kornievskaia
Quoted 16 lines · Al Viro
Quoted 14 lines · Olga Kornievskaia
Quoted 12 lines · Al Viro

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

Quoted 8 lines
Quoted 2 lines

This inode is created to make an appearance of an opened file to do (an NFS) read, it's never a directory.

Er... Where does the fhandle come from? From my reading it's a client-sent data; I don't know what trust model do you assume, but the price of getting multiple dentries over the same directory inode is high. Bogus or compromised client should not be able to cause severe corruption of kernel data structures...

Quoted 6 lines

This is an NFS spec specified operation. The (source file's) filehandle comes from the COPY operation compound that the destination server gets and then uses -- creates an inode from using the code you are looking at now -- to access from the source server. Security is all described in the spec. The uniqueness of the filehandle is provided by the source server that created it.

Do we assume that compromise of source server is escalatable at least to kernel panics on the destination server anyway? Confused...

#5 lore raw
In reply to Olga Kornievskaia
Quoted 27 lines · Olga Kornievskaia
Quoted 16 lines · Al Viro
Quoted 14 lines · Olga Kornievskaia
Quoted 12 lines · Al Viro

My apologies for having missed that back when the SSC
patchset had been done (and missing the problems after it got merged, actually).

1) if this

        r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);

in __nfs42_ssc_open() yields a directory inode, we are screwed as soon as it's passed to alloc_file_pseudo() - a *lot* of places in dcache handling would break if we do that. It's not too nice for a regular file from non-cooperating filesystem, but for directory ones it's deadly.

Quoted 8 lines
Quoted 2 lines

This inode is created to make an appearance of an opened file to do (an NFS) read, it's never a directory.

Er... Where does the fhandle come from? From my reading it's a client-sent data; I don't know what trust model do you assume, but the price of getting multiple dentries over the same directory inode is high. Bogus or compromised client should not be able to cause severe corruption of kernel data structures...

Quoted 6 lines

This is an NFS spec specified operation. The (source file's) filehandle comes from the COPY operation compound that the destination server gets and then uses -- creates an inode from using the code you are looking at now -- to access from the source server. Security is all described in the spec. The uniqueness of the filehandle is provided by the source server that created it.

Olga,

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Thanks,
Amir.

author maintainer
#6 lore raw
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

#7 lore raw
Quoted 10 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct

vfsmount *ss_mnt,

                goto out;
        }

+ if (S_ISDIR(fattr->mode)) {
+ res = ERR_PTR(-EBADF);
+ goto out;
+ }
+

        res = ERR_PTR(-ENOMEM);
        len = strlen(SSC_READ_NAME_BODY) + 16;
        read_name = kzalloc(len, GFP_KERNEL);
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
vfsmount *ss_mnt,
                                     r_ino->i_fop);
        if (IS_ERR(filep)) {
                res = ERR_CAST(filep);
+               iput(r_ino);
                goto out_free_name;
        }
#8 lore raw
Quoted 25 lines · Olga Kornievskaia
Quoted 10 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 347

Quoted 8 lines
fs/nfs/nfs4file.c +4 -0
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                 goto out;
         }
 
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }

You're better off going with 'if (!S_ISREG())' so that we also reject symlinks, pipes, devices, etc. The requirement for the NFSv4.2 copy offload protocol is that both the source and destination be regular files.

Quoted 12 lines
fs/nfs/nfs4file.c +1 -0
+
         res = ERR_PTR(-ENOMEM);
         len = strlen(SSC_READ_NAME_BODY) + 16;

        read_name = kzalloc(len, GFP_KERNEL);

fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                      r_ino->i_fop);
         if (IS_ERR(filep)) {
                 res = ERR_CAST(filep);
+               iput(r_ino);
                 goto out_free_name;
         }
Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#9 lore raw
In reply to Olga Kornievskaia
Quoted 21 lines · Olga Kornievskaia
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 348

Quoted 9 lines
fs/nfs/nfs4file.c +5 -0
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                 goto out;
         }
 
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }
+

Can we also enhance nfsd4_do_async_copy to check for -EBADF and returns nfserr_wrong_type? perhaps adding an error mapping function to handle other errors also.

-Dai

Quoted 11 lines
fs/nfs/nfs4file.c +0 -0
         res = ERR_PTR(-ENOMEM);
         len = strlen(SSC_READ_NAME_BODY) + 16;
         read_name = kzalloc(len, GFP_KERNEL);
fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                      r_ino->i_fop);
         if (IS_ERR(filep)) {
                 res = ERR_CAST(filep);
+               iput(r_ino);
                 goto out_free_name;
         }
#10 lore raw
Quoted 28 lines · On Thu, Aug 18, 2022 at 10:52 PM <[email protected]> wrote:
Quoted 25 lines · Olga Kornievskaia
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                 goto out;
         }
 
Quoted 9 lines

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 348

Quoted 5 lines
fs/nfs/nfs4file.c +5 -0
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }
+

Can we also enhance nfsd4_do_async_copy to check for -EBADF and returns nfserr_wrong_type? perhaps adding an error mapping function to handle other errors also.

On the server side, if the open fails that's already translated into the appropriate error -- err_off_load_denied.

Quoted 14 lines

-Dai

Quoted 11 lines
fs/nfs/nfs4file.c +0 -0
         res = ERR_PTR(-ENOMEM);
         len = strlen(SSC_READ_NAME_BODY) + 16;
         read_name = kzalloc(len, GFP_KERNEL);
fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                      r_ino->i_fop);
         if (IS_ERR(filep)) {
                 res = ERR_CAST(filep);
+               iput(r_ino);
                 goto out_free_name;
         }
#11 lore raw
Quoted 36 lines · Olga Kornievskaia
Quoted 27 lines · On Thu, Aug 18, 2022 at 10:52 PM <[email protected]> wrote:
Quoted 25 lines · Olga Kornievskaia
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                  goto out;
          }
 
Quoted 8 lines

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 348

Quoted 5 lines
fs/nfs/nfs4file.c +5 -0
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }
+

Can we also enhance nfsd4_do_async_copy to check for -EBADF and returns nfserr_wrong_type? perhaps adding an error mapping function to handle other errors also.

Quoted 2 lines

On the server side, if the open fails that's already translated into the appropriate error -- err_off_load_denied.

Currently the server returns nfserr_offload_denied if the open fails for any reasons. I'm wondering whether the server should return more accurate error code such as if the source file handle is a wrong type then the server should return nfserr_wrong_type, instead of nfserr_offload_denied, to match the spec:

Both SAVED_FH and CURRENT_FH must be regular files. If either SAVED_FH or CURRENT_FH is not a regular file, the operation MUST fail and return NFS4ERR_WRONG_TYPE.

-Dai

Quoted 14 lines
Quoted 14 lines

-Dai

Quoted 11 lines
fs/nfs/nfs4file.c +0 -0
          res = ERR_PTR(-ENOMEM);
          len = strlen(SSC_READ_NAME_BODY) + 16;
          read_name = kzalloc(len, GFP_KERNEL);
fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                       r_ino->i_fop);
          if (IS_ERR(filep)) {
                  res = ERR_CAST(filep);
+               iput(r_ino);
                  goto out_free_name;
          }
#12 lore raw
Quoted 48 lines · On Fri, Aug 19, 2022 at 11:42 AM <[email protected]> wrote:
Quoted 36 lines · Olga Kornievskaia
Quoted 27 lines · On Thu, Aug 18, 2022 at 10:52 PM <[email protected]> wrote:
Quoted 25 lines · Olga Kornievskaia
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                  goto out;
          }
 
Quoted 8 lines

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 348

Quoted 5 lines
fs/nfs/nfs4file.c +5 -0
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }
+

Can we also enhance nfsd4_do_async_copy to check for -EBADF and returns nfserr_wrong_type? perhaps adding an error mapping function to handle other errors also.

Quoted 2 lines

On the server side, if the open fails that's already translated into the appropriate error -- err_off_load_denied.

Currently the server returns nfserr_offload_denied if the open fails for any reasons. I'm wondering whether the server should return more accurate error code such as if the source file handle is a wrong type then the server should return nfserr_wrong_type, instead of nfserr_offload_denied, to match the spec:

Quoted 3 lines

Both SAVED_FH and CURRENT_FH must be regular files. If either SAVED_FH or CURRENT_FH is not a regular file, the operation MUST fail and return NFS4ERR_WRONG_TYPE.

Ok sure. That's a relevant but a separate patch.

Quoted 17 lines

-Dai

Quoted 14 lines
Quoted 14 lines

-Dai

Quoted 11 lines
fs/nfs/nfs4file.c +0 -0
          res = ERR_PTR(-ENOMEM);
          len = strlen(SSC_READ_NAME_BODY) + 16;
          read_name = kzalloc(len, GFP_KERNEL);
fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                       r_ino->i_fop);
          if (IS_ERR(filep)) {
                  res = ERR_CAST(filep);
+               iput(r_ino);
                  goto out_free_name;
          }
#13 lore raw
Quoted 46 lines · Olga Kornievskaia
Quoted 45 lines · On Fri, Aug 19, 2022 at 11:42 AM <[email protected]> wrote:
Quoted 35 lines · Olga Kornievskaia
Quoted 26 lines · On Thu, Aug 18, 2022 at 10:52 PM <[email protected]> wrote:
Quoted 25 lines · Olga Kornievskaia
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

NFS spec does not guarantee the safety of the server. It's like saying that the Law makes Crime impossible. The law needs to be enforced, so if server gets a request to COPY from/to an fhandle that resolves as a non-regular file (from a rogue or buggy NFS client) the server should return an error and not continue to alloc_file_pseudo().

Quoted 5 lines

FWIW, my preference would be to have alloc_file_pseudo() reject directory inodes if it ever gets such.

I'm still not sure that my (and yours, apparently) interpretation of what Olga said is correct, though.

Would it be appropriate to do the following then:

fs/nfs/nfs4file.c +0 -0
index e88f6b18445e..112134b6438d 100644
@@ -340,6 +340,11 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                   goto out;
           }
 
Quoted 8 lines

fs/nfs/nfs4file.c static struct file *__nfs42_ssc_open(struct · line 348

Quoted 5 lines
fs/nfs/nfs4file.c +5 -0
+       if (S_ISDIR(fattr->mode)) {
+               res = ERR_PTR(-EBADF);
+               goto out;
+       }
+

Can we also enhance nfsd4_do_async_copy to check for -EBADF and returns nfserr_wrong_type? perhaps adding an error mapping function to handle other errors also.

Quoted 2 lines

On the server side, if the open fails that's already translated into the appropriate error -- err_off_load_denied.

Currently the server returns nfserr_offload_denied if the open fails for any reasons. I'm wondering whether the server should return more accurate error code such as if the source file handle is a wrong type then the server should return nfserr_wrong_type, instead of nfserr_offload_denied, to match the spec:

Quoted 4 lines
Quoted 3 lines

Both SAVED_FH and CURRENT_FH must be regular files. If either SAVED_FH or CURRENT_FH is not a regular file, the operation MUST fail and return NFS4ERR_WRONG_TYPE.

Ok sure. That's a relevant but a separate patch.

Thank you Olga!

-Dai

Quoted 16 lines
Quoted 16 lines

-Dai

Quoted 13 lines
Quoted 13 lines

-Dai

Quoted 11 lines
fs/nfs/nfs4file.c +0 -0
           res = ERR_PTR(-ENOMEM);
           len = strlen(SSC_READ_NAME_BODY) + 16;
           read_name = kzalloc(len, GFP_KERNEL);
fs/nfs/nfs4file.c +1 -0
@@ -357,6 +362,7 @@ static struct file *__nfs42_ssc_open(struct
 vfsmount *ss_mnt,
                                        r_ino->i_fop);
           if (IS_ERR(filep)) {
                   res = ERR_CAST(filep);
+               iput(r_ino);
                   goto out_free_name;
           }