diff mbox series

[v2] fcntl: Add 32bit filesystem mode

Message ID 20200529072017.2906-1-linus.walleij@linaro.org
State Superseded
Headers show
Series [v2] fcntl: Add 32bit filesystem mode | expand

Commit Message

Linus Walleij May 29, 2020, 7:20 a.m. UTC
It was brought to my attention that this bug from 2018 was
still unresolved: 32 bit emulators like QEMU were given
64 bit hashes when running 32 bit emulation on 64 bit systems.

This adds a flag to the fcntl() F_GETFD and F_SETFD operations
to set the underlying filesystem into 32bit mode even if the
file handle was opened using 64bit mode without the compat
syscalls.

Programs that need the 32 bit file system behavior need to
issue a fcntl() system call such as in this example:

  #define FD_32BIT_MODE 2

  int main(int argc, char** argv) {
    DIR* dir;
    int err;
    int fd;

    dir = opendir("/boot");
    fd = dirfd(dir);
    err = fcntl(fd, F_SETFD, FD_32BIT_MODE);
    if (err) {
      printf("fcntl() failed! err=%d\n", err);
      return 1;
    }
    printf("dir=%p\n", dir);
    printf("readdir(dir)=%p\n", readdir(dir));
    printf("errno=%d: %s\n", errno, strerror(errno));
    return 0;
  }

This can be pretty hard to test since C libraries and linux
userspace security extensions aggressively filter the parameters
that are passed down and allowed to commit into actual system
calls.

Cc: Florian Weimer <fw@deneb.enyo.de>
Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: Andy Lutomirski <luto@kernel.org>
Suggested-by: Theodore Ts'o <tytso@mit.edu>
Link: https://bugs.launchpad.net/qemu/+bug/1805913
Link: https://lore.kernel.org/lkml/87bm56vqg4.fsf@mid.deneb.enyo.de/
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=205957
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

---
ChangeLog v1->v2:
- Use a new flag FD_32BIT_MODE to F_GETFD and F_SETFD
  instead of a new fcntl operation, there is already a fcntl
  operation to set random flags.
- Sorry for taking forever to respin this patch :(
---
 fs/fcntl.c                       | 5 +++++
 include/uapi/asm-generic/fcntl.h | 8 ++++++++
 2 files changed, 13 insertions(+)

-- 
2.25.4

Comments

Peter Maydell June 23, 2020, 10:08 a.m. UTC | #1
On Fri, 29 May 2020 at 08:22, Linus Walleij <linus.walleij@linaro.org> wrote:
>

> It was brought to my attention that this bug from 2018 was

> still unresolved: 32 bit emulators like QEMU were given

> 64 bit hashes when running 32 bit emulation on 64 bit systems.

>

> This adds a flag to the fcntl() F_GETFD and F_SETFD operations

> to set the underlying filesystem into 32bit mode even if the

> file handle was opened using 64bit mode without the compat

> syscalls.


I somewhat belatedly got round to updating my QEMU patch
that uses this new fcntl() flag to fix the bug. Sorry for
the delay getting round to this. You can find the QEMU patch here:
https://patchew.org/QEMU/20200623100101.6041-1-peter.maydell@linaro.org/
(it's an RFC because obviously we won't put it into QEMU until
the kernel side has gone upstream and the API is final.)

What's the next step for moving this forward?

thanks
-- PMM
Linus Walleij July 6, 2020, 8:54 a.m. UTC | #2
On Tue, Jun 23, 2020 at 12:08 PM Peter Maydell <peter.maydell@linaro.org> wrote:
> On Fri, 29 May 2020 at 08:22, Linus Walleij <linus.walleij@linaro.org> wrote:

> >

> > It was brought to my attention that this bug from 2018 was

> > still unresolved: 32 bit emulators like QEMU were given

> > 64 bit hashes when running 32 bit emulation on 64 bit systems.

> >

> > This adds a flag to the fcntl() F_GETFD and F_SETFD operations

> > to set the underlying filesystem into 32bit mode even if the

> > file handle was opened using 64bit mode without the compat

> > syscalls.

>

> I somewhat belatedly got round to updating my QEMU patch

> that uses this new fcntl() flag to fix the bug. Sorry for

> the delay getting round to this. You can find the QEMU patch here:

> https://patchew.org/QEMU/20200623100101.6041-1-peter.maydell@linaro.org/

> (it's an RFC because obviously we won't put it into QEMU until

> the kernel side has gone upstream and the API is final.)

>

> What's the next step for moving this forward?


Ted, can you merge this patch?

It seems QEMU is happy and AFICT it uses the approach you want :)

Yours,
Linus Walleij
Linus Walleij July 19, 2020, 12:34 p.m. UTC | #3
On Mon, Jul 6, 2020 at 10:54 AM Linus Walleij <linus.walleij@linaro.org> wrote:

> Ted, can you merge this patch?

>

> It seems QEMU is happy and AFICT it uses the approach you want :)


Gentle ping!

Yours,
Linus Walleij
Linus Walleij Aug. 6, 2020, 10:47 a.m. UTC | #4
On Sun, Jul 19, 2020 at 2:34 PM Linus Walleij <linus.walleij@linaro.org> wrote:
> On Mon, Jul 6, 2020 at 10:54 AM Linus Walleij <linus.walleij@linaro.org> wrote:

>

> > Ted, can you merge this patch?

> >

> > It seems QEMU is happy and AFICT it uses the approach you want :)

>

> Gentle ping!


Special merge-window ping.

Shall I resend the patch?

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/fs/fcntl.c b/fs/fcntl.c
index 2e4c0fa2074b..10a6b712d3a7 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -335,10 +335,15 @@  static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 		break;
 	case F_GETFD:
 		err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
+		/* Report 32bit file system mode */
+		if (filp->f_mode & FMODE_32BITHASH)
+			err |= FD_32BIT_MODE;
 		break;
 	case F_SETFD:
 		err = 0;
 		set_close_on_exec(fd, arg & FD_CLOEXEC);
+		if (arg & FD_32BIT_MODE)
+			filp->f_mode |= FMODE_32BITHASH;
 		break;
 	case F_GETFL:
 		err = filp->f_flags;
diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
index 9dc0bf0c5a6e..edd3573cb7ef 100644
--- a/include/uapi/asm-generic/fcntl.h
+++ b/include/uapi/asm-generic/fcntl.h
@@ -160,6 +160,14 @@  struct f_owner_ex {
 
 /* for F_[GET|SET]FL */
 #define FD_CLOEXEC	1	/* actually anything with low bit set goes */
+/*
+ * This instructs the kernel to provide 32bit semantics (such as hashes) from
+ * the file system layer, when running a userland that depend on 32bit
+ * semantics on a kernel that supports 64bit userland, but does not use the
+ * compat ioctl() for e.g. open(), so that the kernel would otherwise assume
+ * that the userland process is capable of dealing with 64bit semantics.
+ */
+#define FD_32BIT_MODE	2
 
 /* for posix fcntl() and lockf() */
 #ifndef F_RDLCK