[PATCH v3] blkdiscard: check that the file is a block device before opening

Ari Sundholm ari at tuxera.com
Thu Feb 4 13:03:53 UTC 2016


Hi!

On Mon, 2016-02-01 at 01:59 +0000, Denys Vlasenko wrote:
> I don't understand the problem you are trying to solve.

>From the man page of open(2):
"In general, the behavior of O_EXCL is undefined if it is used without
O_CREAT. There is one exception: on Linux 2.6 and later, O_EXCL can be
used without O_CREAT if pathname refers to a block device."

My point is nothing more and nothing less than that opening a file with
O_EXCL without O_CREAT yields undefined behavior unless the file is a
block device. I know that this happens to work in practice with the
current Linux kernel versions, but theoretically future kernel versions
may validly do something entirely different. I am not convinced even the
"don't break userspace" rule would prevent this for cases of explicitly
stated undefined behavior. Or maybe it will, who knows? The key is the
uncertainty.

I'd err on the side of caution, but this is ultimately your call.

Best regards,
Ari Sundholm
ari at tuxera.com

> Let's try using blkdiscard on a regular file:
> 
> # strace ./busybox blkdiscard README
> execve("./busybox", ["./busybox", "blkdiscard", "README"], [/* 33 vars */]) = 0
> ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or
> TCGETS, {B38400 opost isig icanon echo ...}) = 0
> ioctl(1, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or
> TCGETS, {B38400 opost isig icanon echo ...}) = 0
> getuid32()                              = 0
> open("README", O_RDWR|O_EXCL|O_LARGEFILE) = 3
> ioctl(3, BLKGETSIZE64, 0xffcc5af0)      = -1 ENOTTY (Inappropriate
> ioctl for device)
> brk(0)                                  = 0x8129000
> brk(0x812a000)                          = 0x812a000
> write(2, "blkdiscard: ioctl 0x80041272 fai"..., 68blkdiscard: ioctl
> 0x80041272 failed: Inappropriate ioctl for device
> ) = 68
> _exit(1)                                = ?
> +++ exited with 1 +++
> 
> So, open(O_RDWR|O_EXCL) did work in practice, by ignoring O_EXCL.




More information about the busybox mailing list