Skip to contentSkip to conversation

Why does OUT_FENCE_PTR point not to an fd (s32) but to an s64?

DISCUSSION Chad Versace 4 participants 9 messages Jan 6, 2017 – Jan 13, 2017

lore
Share & tools
lore
#0 lore raw

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

For reference, here's the relevant DRM code.

    // file: drivers/gpu/drm/drm_atomic.c
    struct drm_out_fence_state {
            s64 __user *out_fence_ptr;
            struct sync_file *sync_file;
            int fd;
    };
    
    static int setup_out_fence(struct drm_out_fence_state *fence_state,
                               struct dma_fence *fence)
    {
            fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
            if (fence_state->fd < 0)
                    return fence_state->fd;
    
            if (put_user(fence_state->fd, fence_state->out_fence_ptr))
                    return -EFAULT;
    
            fence_state->sync_file = sync_file_create(fence);
            if (!fence_state->sync_file)
                    return -ENOMEM;
    
            return 0;
    }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw

+rantogno

Rafael, I finally discovered the source of the bug I was hitting.

Quoted 34 lines · Chad Versace

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

For reference, here's the relevant DRM code.

    // file: drivers/gpu/drm/drm_atomic.c
    struct drm_out_fence_state {
            s64 __user *out_fence_ptr;
            struct sync_file *sync_file;
            int fd;
    };
    
    static int setup_out_fence(struct drm_out_fence_state *fence_state,
                               struct dma_fence *fence)
    {
            fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
            if (fence_state->fd < 0)
                    return fence_state->fd;
    
            if (put_user(fence_state->fd, fence_state->out_fence_ptr))
                    return -EFAULT;
    
            fence_state->sync_file = sync_file_create(fence);
            if (!fence_state->sync_file)
                    return -ENOMEM;
    
            return 0;
    }
Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
In reply to Chad Versace
Quoted 8 lines · Chad Versace

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit, while it works on 32 bit. "int" doesn't have that problem directly, but it's still a red flag imo.
-Daniel

Quoted 26 lines

For reference, here's the relevant DRM code.

    // file: drivers/gpu/drm/drm_atomic.c
    struct drm_out_fence_state {
            s64 __user *out_fence_ptr;
            struct sync_file *sync_file;
            int fd;
    };
    
    static int setup_out_fence(struct drm_out_fence_state *fence_state,
                               struct dma_fence *fence)
    {
            fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
            if (fence_state->fd < 0)
                    return fence_state->fd;
    
            if (put_user(fence_state->fd, fence_state->out_fence_ptr))
                    return -EFAULT;
    
            fence_state->sync_file = sync_file_create(fence);
            if (!fence_state->sync_file)
                    return -ENOMEM;
    
            return 0;
    }
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Hi Daniel,

Quoted 4 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 3 lines · Chad Versace

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

Quoted 8 lines
Quoted 5 lines

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit,

Why so ? And why do we care ? The commonly accepted practice is to store file descriptors in int variables. s32 is an int on all platforms, so that's fine too. If we use a s32 pointer here, and someone decides to store it in a long, bool or cast it to a complex, that's their problem :-)

Quoted 28 lines

while it works on 32 bit. "int" doesn't have that problem directly, but it's still a red flag imo.

Quoted 25 lines

For reference, here's the relevant DRM code.

    // file: drivers/gpu/drm/drm_atomic.c
    struct drm_out_fence_state {
            s64 __user *out_fence_ptr;
            struct sync_file *sync_file;
            int fd;
    };
    
    static int setup_out_fence(struct drm_out_fence_state *fence_state,
                               struct dma_fence *fence)
    {
            fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
            if (fence_state->fd < 0)
                    return fence_state->fd;
            
            if (put_user(fence_state->fd, fence_state->out_fence_ptr))
                    return -EFAULT;
            
            fence_state->sync_file = sync_file_create(fence);
            if (!fence_state->sync_file)
                    return -ENOMEM;
            
            return 0;
    }
Signature · 8 lines
Regards,

Laurent Pinchart

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

2017-01-10 Laurent Pinchart <[email protected]>:

Quoted 13 lines

Hi Daniel,

Quoted 10 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 8 lines · Chad Versace

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Quoted 7 lines
Quoted 2 lines

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit,

Why so ? And why do we care ? The commonly accepted practice is to store file descriptors in int variables. s32 is an int on all platforms, so that's fine too. If we use a s32 pointer here, and someone decides to store it in a long, bool or cast it to a complex, that's their problem :-)

The only thing that really needs to be s64 here is the OUT_FENCE_PTR property in the Atomic interface because we carry a pointer there, but all the manipulation after that is actually done after can easily be done on s32 or int.

We can't expect that userspace will know that we store as s64 and clear the bits above if a int was passed down. if we use s32 we will be in complaince with other linux apis that deals with fds. I'd say we fix this before it can cause more damage out there.

Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 28 lines · Gustavo Padovan

2017-01-10 Laurent Pinchart <[email protected]>:

Quoted 13 lines

Hi Daniel,

Quoted 10 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 8 lines · Chad Versace

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Quoted 7 lines
Quoted 2 lines

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit,

Why so ? And why do we care ? The commonly accepted practice is to store file descriptors in int variables. s32 is an int on all platforms, so that's fine too. If we use a s32 pointer here, and someone decides to store it in a long, bool or cast it to a complex, that's their problem :-)

The only thing that really needs to be s64 here is the OUT_FENCE_PTR property in the Atomic interface because we carry a pointer there, but all the manipulation after that is actually done after can easily be done on s32 or int.

Quoted 4 lines

We can't expect that userspace will know that we store as s64 and clear the bits above if a int was passed down. if we use s32 we will be in complaince with other linux apis that deals with fds. I'd say we fix this before it can cause more damage out there.

Changing uabi is kinda tricky, but it's still very new, so if we make sure it gets applied everywhere and doesn't accidentally ship we can it. Iirc fences are only in 4.10, so we should be fine ...
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

Hi Daniel,

Quoted 28 lines · On Thursday 12 Jan 2017 20:26:40 Daniel Vetter wrote:
Quoted 27 lines · Gustavo Padovan

2017-01-10 Laurent Pinchart <[email protected]>:

Quoted 15 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 10 lines · Chad Versace
Quoted 8 lines

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Quoted 3 lines

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit,

Quoted 5 lines

Why so ? And why do we care ? The commonly accepted practice is to store file descriptors in int variables. s32 is an int on all platforms, so that's fine too. If we use a s32 pointer here, and someone decides to store it in a long, bool or cast it to a complex, that's their problem :-)

The only thing that really needs to be s64 here is the OUT_FENCE_PTR property in the Atomic interface because we carry a pointer there, but all the manipulation after that is actually done after can easily be done on s32 or int.

Quoted 8 lines
Quoted 4 lines

We can't expect that userspace will know that we store as s64 and clear the bits above if a int was passed down. if we use s32 we will be in complaince with other linux apis that deals with fds. I'd say we fix this before it can cause more damage out there.

Changing uabi is kinda tricky, but it's still very new, so if we make sure it gets applied everywhere and doesn't accidentally ship we can it. Iirc fences are only in 4.10, so we should be fine ...

Correct. That sounds good to me, there's still time for a v4.10-rc fix.

Signature · 8 lines
Regards,

Laurent Pinchart

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#7 lore raw

2017-01-12 Laurent Pinchart <[email protected]>:

Quoted 36 lines

Hi Daniel,

Quoted 33 lines · On Thursday 12 Jan 2017 20:26:40 Daniel Vetter wrote:
Quoted 31 lines · Gustavo Padovan

2017-01-10 Laurent Pinchart <[email protected]>:

Quoted 15 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 10 lines · Chad Versace
Quoted 8 lines

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

I naïvely expected that OUT_FENCE_PTR would be a pointer to, obviously, a fence fd (s32 __user *). But it's not. It's s64 __user *. Due to that surprise, I spent several hours chasing down weird corruption in Rob Clark's kmscube. The kernel unexpectedly cleared the 32 bits *above* an int kms_fence_fd in userspace.

Quoted 3 lines

Never use unsized types for uabi. I guess we could have used s32, but then someone is going to store this in a long and it goes boom on 64 bit,

Quoted 5 lines

Why so ? And why do we care ? The commonly accepted practice is to store file descriptors in int variables. s32 is an int on all platforms, so that's fine too. If we use a s32 pointer here, and someone decides to store it in a long, bool or cast it to a complex, that's their problem :-)

The only thing that really needs to be s64 here is the OUT_FENCE_PTR property in the Atomic interface because we carry a pointer there, but all the manipulation after that is actually done after can easily be done on s32 or int.

We can't expect that userspace will know that we store as s64 and clear the bits above if a int was passed down. if we use s32 we will be in complaince with other linux apis that deals with fds. I'd say we fix this before it can cause more damage out there.

Quoted 5 lines
Quoted 3 lines

Changing uabi is kinda tricky, but it's still very new, so if we make sure it gets applied everywhere and doesn't accidentally ship we can it. Iirc fences are only in 4.10, so we should be fine ...

Correct. That sounds good to me, there's still time for a v4.10-rc fix.

I'd expect users defining an int and hitting the issue Chad hit instead of going to long types. So I hope we are safe here. I'll prepare a patch to make it s32.

Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw
Quoted 11 lines · Gustavo Padovan

2017-01-12 Laurent Pinchart <[email protected]>:

Quoted 9 lines

Hi Daniel,

Quoted 6 lines · On Thursday 12 Jan 2017 20:26:40 Daniel Vetter wrote:
Quoted 5 lines · Gustavo Padovan

2017-01-10 Laurent Pinchart <[email protected]>:

Quoted 4 lines · On Monday 09 Jan 2017 11:23:23 Daniel Vetter wrote:
Quoted 3 lines · Chad Versace
Quoted 2 lines

Was this a mistake in the API? If so, can we fix this ABI mistake before kernel consumers rely on this?

[...]

Quoted 3 lines

I'd expect users defining an int and hitting the issue Chad hit instead of going to long types. So I hope we are safe here. I'll prepare a patch to make it s32.

Thanks for fixing this. I'm glad we caught it before the uabi became frozen.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel