fbmem: copy_from/to_user() with mutex held (v2)
PATCH 1/1 fbmem: copy_from/to_user() with mutex held (v2)
Avoid to call copy_from/to_user() with fb_info->lock mutex held in fbmem ioctl().
fb_mmap() is called under mm->mmap_sem (A) held, that also acquires fb_info->lock (B); fb_ioctl() takes fb_info->lock (B) and does copy_from/to_user() that might acquire mm->mmap_sem (A), causing a deadlock.
NOTE: it doesn't push down the fb_info->lock in each own driver's fb_ioctl(), so there're still potential deadlocks somewhere.
ChangeLog (v1 -> v2):
* renamed get/put_fb_info() into lock/unlock_fb_info()
* lock/unlock_fb_info() return int instead of struct fb_info
* removed some useless memcpy()
--- drivers/video/fbcmap.c | 20 +++++-- drivers/video/fbmem.c | 132 ++++++++++++++++++++++++----------------------- include/linux/fb.h | 17 ++++++- 3 files changed, 97 insertions(+), 72 deletions(-)
drivers/video/fbcmap.c +14 -6
@@ -250,10 +250,6 @@ int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)int rc, size = cmap->len *sizeof (u16);struct fb_cmap umap; -if (cmap->start <0 || (!info->fbops->fb_setcolreg && - !info->fbops->fb_setcmap)) -return -EINVAL ; -memset (&umap,0 ,sizeof (struct fb_cmap )); rc =fb_alloc_cmap (&umap, cmap->len, cmap->transp !=NULL );if (rc)
@@ -262,11 +258,23 @@ int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info) copy_from_user(umap.green, cmap->green, size) || copy_from_user(umap.blue, cmap->blue, size) || (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) { -fb_dealloc_cmap (&umap); -return -EFAULT ; + rc = -EFAULT ; + goto out; } umap.start = cmap->start; +if (!lock_fb_info (info)) { + rc = -ENODEV ; + goto out; + } +if (cmap->start <0 || (!info->fbops->fb_setcolreg && + !info->fbops->fb_setcmap)) { + rc = -EINVAL ; + goto out1; + } rc =fb_set_cmap (&umap, info); +out1: +unlock_fb_info (info); +out:fb_dealloc_cmap (&umap);return rc; }
drivers/video/fbmem.c +67 -65
218 lines not on this page · raw message
include/linux/fb.h +16 -1
@@ -960,6 +960,21 @@ extern struct fb_info *registered_fb[FB_MAX];extern int num_registered_fb;extern struct class *fb_class; +static inline int lock_fb_info (struct fb_info *info) +{ +mutex_lock (&info->lock); +if (!info->fbops) { +mutex_unlock (&info->lock); +return 0 ; + } +return 1 ; +} + +static inline void unlock_fb_info (struct fb_info *info) +{ +mutex_unlock (&info->lock); +} +static inline void __fb_pad_aligned_buffer (u8 *dst,u32 d_pitch,u8 *src,u32 s_pitch,u32 height) {
@@ -1068,7 +1083,7 @@ extern void fb_dealloc_cmap(struct fb_cmap *cmap);extern int fb_copy_cmap (const struct fb_cmap *from,struct fb_cmap *to);extern int fb_cmap_to_user (const struct fb_cmap *from,struct fb_cmap_user *to);extern int fb_set_cmap (struct fb_cmap *cmap,struct fb_info *fb_info); -extern int fb_set_user_cmap (struct fb_cmap_user *cmap,struct fb_info *fb_info); +extern int fb_set_user_cmap (struct fb_cmap_user *cmap,struct fb_info *info);extern const struct fb_cmap *fb_default_cmap (int len);extern void fb_invert_cmaps (void );
Signature · 1 line
1.5.6.3
Quoted 17 lines · On Mon, 19 Jan 2009 12:04:04 +0100 Andrea Righi <[email protected]> wrote:
Avoid to call copy_from/to_user() with
fb_info->lockmutex held in fbmemioctl().
fb_mmap()is called undermm->mmap_sem(A) held, that also acquiresfb_info->lock(B);fb_ioctl()takesfb_info->lock(B) and does copy_from/to_user() that might acquiremm->mmap_sem(A), causing a deadlock.NOTE: it doesn't push down the
fb_info->lockin each own driver'sfb_ioctl(), so there're still potential deadlocks somewhere.drivers/video/fbmem.c +0
-0@@ -1013,25 +1013,26 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
(...)
Quoted 13 lines
drivers/video/fbmem.c +4
-4case FBIOPUT_VSCREENINFO : -if (copy_from_user (&var, argp,sizeof (var))) { - ret = -EFAULT ; -break ; - } +if (copy_from_user (&var, argp,sizeof (var))) +return -EFAULT ; +if (!lock_fb_info (info)) +return -ENODEV ;acquire_console_sem (); info->flags |=FBINFO_MISC_USEREVENT ; ret =fb_set_var (info, &var);
drivers/video/fbmem.c static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, · line 1043
Quoted 2 lines
drivers/video/fbmem.c +0
-0@@ -1041,104 +1042,109 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, ret = -EFAULT ;break ;
I don't see a call to unlock_fb_info(info) in the case for FBIOPUT_VSCREENINFO. Is it intentional?
Kind regards,
Krzysztof
Quoted 31 lines · Krzysztof Helt
Quoted 17 lines · On Mon, 19 Jan 2009 12:04:04 +0100 Andrea Righi <[email protected]> wrote:
Avoid to call copy_from/to_user() with
fb_info->lockmutex held in fbmemioctl().
fb_mmap()is called undermm->mmap_sem(A) held, that also acquiresfb_info->lock(B);fb_ioctl()takesfb_info->lock(B) and does copy_from/to_user() that might acquiremm->mmap_sem(A), causing a deadlock.NOTE: it doesn't push down the
fb_info->lockin each own driver'sfb_ioctl(), so there're still potential deadlocks somewhere.drivers/video/fbmem.c +0
-0@@ -1013,25 +1013,26 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,(...)
Quoted 10 lines
case
FBIOPUT_VSCREENINFO:drivers/video/fbmem.c +4
-4-if (copy_from_user (&var, argp,sizeof (var))) { - ret = -EFAULT ; -break ; - } +if (copy_from_user (&var, argp,sizeof (var))) +return -EFAULT ; +if (!lock_fb_info (info)) +return -ENODEV ;acquire_console_sem ();
Quoted 8 lines
drivers/video/fbmem.c static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, · line 1043
Quoted 5 lines
info->flags |= FBINFO_MISC_USEREVENT; ret = fb_set_var(info, &var);drivers/video/fbmem.c +0
-0@@ -1041,104 +1042,109 @@ static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, ret = -EFAULT ;break ;I don't see a call to
unlock_fb_info(info)in the case forFBIOPUT_VSCREENINFO. Is it intentional?
No, it's a bug :(. I'll post a new patch soon.
Thanks for reviewing!
-Andrea
Quoted 3 lines
Kind regards,
Krzysztof