Several device-mapper fixes
Subsystems (2)
cover Several device-mapper fixes
This series contains several miscellaneous fixes to input validation in device-mapper. The only potentially controversial commits should be the last two, which prevent creating devices named ".", "..", or "control". The other patches fix input validation problems that have existed since at least the beginning of git history.
Demi Marie Obenour (6):
device-mapper: Check that target specs are sufficiently aligned
device-mapper: Avoid pointer arithmetic overflow
device-mapper: structs and parameter strings must not overlap
device-mapper: Avoid double-fetch of version
device-mapper: Refuse to create device named "control"
device-mapper: "." and ".." are not valid symlink names
drivers/md/dm-ioctl.c | 75 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 12 deletions(-)
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 1/6 device-mapper: Check that target specs are sufficiently aligned
Otherwise subsequent code will dereference a misaligned struct dm_target_spec *, which is undefined behavior.
--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/md/dm-ioctl.c +7 -0
@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" ); +if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 2/6 device-mapper: Avoid pointer arithmetic overflow
In reply to Demi Marie ObenourEspecially on 32-bit systems, it is possible for the pointer arithmetic to overflow and cause a userspace pointer to be dereferenced in the kernel.
--- drivers/md/dm-ioctl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/md/dm-ioctl.c +19 -0
@@ -1396,6 +1396,25 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, {static_assert (_Alignof(struct dm_target_spec ) <=8 ,"struct dm_target_spec has excessive alignment requirements" ); +static_assert (offsetof(struct dm_ioctl , data) >=sizeof (struct dm_target_spec ), +"struct dm_target_spec too big" ); + +/* +* Number of bytes remaining, starting with last. This is always +* sizeof(struct dm_target_spec) or more, as otherwise *last was +* out of bounds already. +*/ +size_t remaining = (char *)end - (char *)last; + +/* +* There must be room for both the next target spec and the +* NUL-terminator of the target itself. +*/ +if (remaining -sizeof (struct dm_target_spec ) <= next) { +DMERR ("Target spec extends beyond end of parameters" ); +return -EINVAL ; + } +if (next %8 ) {DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next);return -EINVAL ;
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 3/6 device-mapper: structs and parameter strings must not overlap
In reply to Demi Marie ObenourThe NUL terminator for each target parameter string must precede the following 'struct dm_target_spec'. Otherwise, dm_split_args() might corrupt this struct. Furthermore, the first 'struct dm_target_spec' must come after the 'struct dm_ioctl', as if it overlaps too much dm_split_args() could corrupt the 'struct dm_ioctl'.
--- drivers/md/dm-ioctl.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/md/dm-ioctl.c +21 -7
@@ -1391,7 +1391,7 @@ static inline fmode_t get_mode(struct dm_ioctl *param)return mode; } -static int next_target (struct dm_target_spec *last,uint32_t next,void *end, +static int next_target (struct dm_target_spec *last,uint32_t next,const char *end,struct dm_target_spec **spec,char **target_params) { static_assert(_Alignof(struct dm_target_spec) <= 8,
@@ -1404,7 +1404,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end,* sizeof(struct dm_target_spec) or more, as otherwise *last was * out of bounds already. */ -size_t remaining = (char *)end - (char *)last; +size_t remaining = end - (char *)last;/* * There must be room for both the next target spec and the
@@ -1423,10 +1423,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 ); -if (*spec < (last +1 )) -return -EINVAL ; - -return invalid_str (*target_params, end); +return 0 ; } static int populate_table(struct dm_table *table,
@@ -1436,8 +1433,9 @@ static int populate_table(struct dm_table *table,unsigned int i =0 ;struct dm_target_spec *spec = (struct dm_target_spec *) param;uint32_t next = param->data_start; -void *end = (void *) param + param_size; +const char *const end = (const char *) param + param_size;char *target_params; +size_t min_size =sizeof (struct dm_ioctl );if (!param->target_count) {DMERR ("%s: no targets specified" , __func__);
@@ -1445,6 +1443,13 @@ static int populate_table(struct dm_table *table, }for (i =0 ; i < param->target_count; i++) { +const char *nul_terminator; + +if (next < min_size) { +DMERR ("%s: next target spec (offset %u) overlaps %s" , + __func__, next, i ?"previous target" :"'struct dm_ioctl'" ); +return -EINVAL ; + } r =next_target (spec, next, end, &spec, &target_params);if (r) {
@@ -1452,6 +1457,15 @@ static int populate_table(struct dm_table *table,return r; } + nul_terminator =memchr (target_params,0 , (size_t )(end - target_params)); +if (nul_terminator ==NULL ) { +DMERR ("%s: target parameters not NUL-terminated" , __func__); +return -EINVAL ; + } + +/* Add 1 for NUL terminator */ + min_size = (size_t )(nul_terminator - (const char *)spec) +1 ; + r = dm_table_add_target(table, spec->target_type, (sector_t) spec->sector_start, (sector_t) spec->length,
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 4/6 device-mapper: Avoid double-fetch of version
In reply to Demi Marie ObenourThe version is fetched once in check_version(), which then does some validation and then overwrites the version in userspace with the API version supported by the kernel. copy_params() then fetches the version from userspace *again*, and this time no validation is done. The result is that the kernel's version number is completely controllable by userspace, provided that userspace can win a race condition.
Fix this flaw by not copying the version back to the kernel the second time. This is not exploitable as the version is not further used in the kernel. However, it could become a problem if future patches start relying on the version field.
--- drivers/md/dm-ioctl.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/md/dm-ioctl.c +9 -5
@@ -1873,12 +1873,13 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)* As well as checking the version compatibility this always * copies the kernel interface version out. */ -static int check_version (unsigned int cmd,struct dm_ioctl __user *user) +static int check_version (unsigned int cmd,struct dm_ioctl __user *user, +struct dm_ioctl *kernel_params) { -uint32_t version[3 ];int r =0 ; +uint32_t *version = kernel_params->version; -if (copy_from_user (version, user->version,sizeof (version))) +if (copy_from_user (version, user->version,sizeof (user->version)))return -EFAULT ; if ((version[0] != DM_VERSION_MAJOR) ||
@@ -1922,7 +1923,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -if (copy_from_user (param_kernel, user, minimum_data_size)) +/* Version has been copied from userspace already, avoid TOCTOU */ +if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), + (char __user *)user +sizeof (param_kernel->version), + minimum_data_size -sizeof (param_kernel->version)))return -EFAULT ;if (param_kernel->data_size < minimum_data_size) {
@@ -2034,7 +2038,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us* Check the interface version passed in. This also * writes out the kernel's interface version. */ - r =check_version (cmd, user); + r =check_version (cmd, user, ¶m_kernel);if (r)return r;
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
kernel test robot
build botRe: [PATCH 4/6] device-mapper: Avoid double-fetch of version
kernel test robot noticed the following build warnings:
Hi Demi,
kernel test robot noticed the following build warnings:
[auto build test WARNING on device-mapper-dm/for-next]
[also build test WARNING on linus/master v6.4-rc4 next-20230602] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Demi-Marie-Obenour/device-mapper-Check-that-target-specs-are-sufficiently-aligned/20230602-052741 base: https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git for-next patch link: https://lore.kernel.org/r/20230601212456.1533-5-demi%40invisiblethingslab.com patch subject: [PATCH 4/6] device-mapper: Avoid double-fetch of version config: x86_64-randconfig-c032-20230531 (https://download.01.org/0day-ci/archive/20230603/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
cocci warnings: (new ones prefixed by >>)
>> drivers/md/dm-ioctl.c:1900:42-48: ERROR: application of sizeof to pointer
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH 4/6] device-mapper: Avoid double-fetch of version
Quoted 17 lines · kernel test robot
Hi Demi,
kernel test robot noticed the following build warnings:
[auto build test WARNING on device-mapper-dm/for-next]
[also build test WARNING on linus/master v6.4-rc4 next-20230602] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/Demi-Marie-Obenour/device-mapper-Check-that-target-specs-are-sufficiently-aligned/20230602-052741 base: https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git for-next patch link: https://lore.kernel.org/r/20230601212456.1533-5-demi%40invisiblethingslab.com patch subject: [PATCH 4/6] device-mapper: Avoid double-fetch of version config: x86_64-randconfig-c032-20230531 (https://download.01.org/0day-ci/archive/20230603/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
Quoted 6 lines
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/cocci warnings: (new ones prefixed by >>)
Quoted 1 line
Quoted 1 line
drivers/md/dm-ioctl.c:1900:42-48: ERROR: application of sizeof to pointer
Ugh, silly mistake: I changed an array to a pointer but did not change the sizeof. Will send a v2 with the fix.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 5/6 device-mapper: Refuse to create device named "control"
In reply to Demi Marie ObenourTypical userspace setups create a symlink under /dev/mapper with the name of the device, but /dev/mapper/control is reserved for the control device. Therefore, trying to create such a device is almost certain to be a userspace bug.
--- drivers/md/dm-ioctl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/md/dm-ioctl.c +6 -1
@@ -767,7 +767,12 @@ static int get_target_version(struct file *filp, struct dm_ioctl *param, size_tstatic int check_name (const char *name) {if (strchr (name,'/' )) { -DMERR ("invalid device name" ); +DMERR ("device name cannot contain '/'" ); +return -EINVAL ; + } + +if (strcmp (name,DM_CONTROL_NODE ) ==0 ) { +DMERR ("device name cannot be \"%s\"" ,DM_CONTROL_NODE );return -EINVAL ; }
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH 6/6 device-mapper: "." and ".." are not valid symlink names
In reply to Demi Marie ObenourUsing either of these is going to greatly confuse userspace, as they are not valid symlink names and so creating the usual /dev/mapper/NAME symlink will not be possible. As creating a device with either of these names is almost certainly a userspace bug, just error out.
--- drivers/md/dm-ioctl.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/dm-ioctl.c +4 -2
@@ -771,8 +771,10 @@ static int check_name(const char *name)return -EINVAL ; } -if (strcmp (name,DM_CONTROL_NODE ) ==0 ) { -DMERR ("device name cannot be \"%s\"" ,DM_CONTROL_NODE ); +if (strcmp (name,DM_CONTROL_NODE ) ==0 || +strcmp (name,"." ) ==0 || +strcmp (name,".." ) ==0 ) { +DMERR ("device name cannot be \"%s\", \".\", or \"..\"" ,DM_CONTROL_NODE );return -EINVAL ; }
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
v2 cover Several device-mapper fixes
In reply to Demi Marie ObenourThis series contains several miscellaneous fixes to input validation in device-mapper. The only potentially controversial commits should be the last two, which prevent creating devices named ".", "..", or "control". The other patches fix input validation problems that have existed since at least the beginning of git history.
Changes since v1:
- Fix silly mistake (using sizeof() on a pointer) caught by 0day bot.
Demi Marie Obenour (6):
device-mapper: Check that target specs are sufficiently aligned
device-mapper: Avoid pointer arithmetic overflow
device-mapper: structs and parameter strings must not overlap
device-mapper: Avoid double-fetch of version
device-mapper: Refuse to create device named "control"
device-mapper: "." and ".." are not valid symlink names
drivers/md/dm-ioctl.c | 91 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 19 deletions(-)
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v2 1/6 device-mapper: Check that target specs are sufficiently aligned
Otherwise subsequent code will dereference a misaligned struct dm_target_spec *, which is undefined behavior.
--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/md/dm-ioctl.c +7 -0
@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" ); +if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Mike Snitzer
maintainerRe: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned
Quoted 15 lines · Demi Marie Obenour
Otherwise subsequent code will dereference a misaligned
struct dm_target_spec *, which is undefined behavior.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/md/dm-ioctl.c +0
-0
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1398
Quoted 5 lines
drivers/md/dm-ioctl.c +2
-0@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" );
Really not sure what you mean by "has excessive alignment requirements"...
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1406
Quoted 8 lines
drivers/md/dm-ioctl.c +5
-0+if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );
But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.
I did pick up patches 4-6 for the upcoming 6.5 merge window.
Note, please prefix with "dm ioctl" instead of "device-mapper".
(I just switched my "dm" prefix to "dm ioctl" and forced update on the dm-6.5 branch, so the commit I referenced earlier for your version copy patch is now here:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-6.5&id=a5a3de762b3ae8959347928843c12502b1b23163
)
Mike
Re: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned
Quoted 13 lines · Mike Snitzer
Quoted 10 lines · Demi Marie Obenour
Otherwise subsequent code will dereference a misaligned
struct dm_target_spec *, which is undefined behavior.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)
Quoted 12 lines
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1398
Quoted 10 lines
drivers/md/dm-ioctl.c +2
-0@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" );Really not sure what you mean by "has excessive alignment requirements"...
This patch checks that struct dm_target_spec is 8-byte aligned. That is okay if its alignment is 8 or less, but not if is 16 or more, so I added a static assert to check that struct dm_target_spec indeed requires at most 8-byte alignment. That said, “excessive alignment requirements” is (as shown by you having to ask this question) a bad error message. Would “must not require more than 8-byte alignment” be better?
Quoted 11 lines
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1406
Quoted 8 lines
drivers/md/dm-ioctl.c +5
-0+if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.
Quoted 1 line
I did pick up patches 4-6 for the upcoming 6.5 merge window.
Thanks!
Quoted 1 line
Note, please prefix with "dm ioctl" instead of "device-mapper".
Good to know, thanks! I have several additional patches written that require patch 4. Should I send patches 1 through 3 in the same series as well?
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Mike Snitzer
maintainerRe: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned
Quoted 27 lines · Demi Marie Obenour
Quoted 13 lines · Mike Snitzer
Quoted 10 lines · Demi Marie Obenour
Otherwise subsequent code will dereference a misaligned
struct dm_target_spec *, which is undefined behavior.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)Quoted 12 lines
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1398
Quoted 10 lines
drivers/md/dm-ioctl.c +2
-0@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" );Really not sure what you mean by "has excessive alignment requirements"...
Quoted 6 lines
This patch checks that struct
dm_target_specis 8-byte aligned. That is okay if its alignment is 8 or less, but not if is 16 or more, so I added a static assert to check that structdm_target_specindeed requires at most 8-byte alignment. That said, “excessive alignment requirements” is (as shown by you having to ask this question) a bad error message. Would “must not require more than 8-byte alignment” be better?
Yes, that's better, I've updated it to use that.
Quoted 13 lines
Quoted 11 lines
drivers/md/dm-ioctl.c static inline fmode_t get_mode(struct dm_ioctl *param) · line 1406
Quoted 8 lines
drivers/md/dm-ioctl.c +5
-0+if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.
Quoted 1 line
I did pick up patches 4-6 for the upcoming 6.5 merge window.
Quoted 7 lines
Thanks!
Quoted 1 line
Note, please prefix with "dm ioctl" instead of "device-mapper".
Good to know, thanks! I have several additional patches written that require patch 4. Should I send patches 1 through 3 in the same series as well?
I did end up picking up patches 1-3 and rebased so they are in front of your patches 4-6 like you intended.
But I agree with Mikulas, I'm not seeing the point in tagging any of these for stable@.
All commits are currently at the tip of dm-6.5, see:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/log/?h=dm-6.5
Mike
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mikulas Patocka
maintainerRe: [dm-devel] [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned
In reply to Demi Marie ObenourQuoted 32 lines · Demi Marie Obenour
Otherwise subsequent code will dereference a misaligned
struct dm_target_spec *, which is undefined behavior.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/md/dm-ioctl.c +7
-0@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" ); +if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Hi
Some architectures (such as 32-bit x86) specify that the alignment of 64-bit integers is only 4-byte. This could in theory break old userspace code that only uses 4-byte alignment. I would change "next % 8" to "next % __alignof__(struct dm_target_spec)".
I think that there is no need to backport this patch series to the stable kernels because the bugs that it fixes may only be exploited by the user with CAP_SYS_ADMIN privilege. So, there is no security or reliability problem being fixed.
Mikulas
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [dm-devel] [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned
Quoted 39 lines · Mikulas Patocka
Quoted 32 lines · Demi Marie Obenour
Otherwise subsequent code will dereference a misaligned
struct dm_target_spec *, which is undefined behavior.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/md/dm-ioctl.c +7
-0@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)static int next_target (struct dm_target_spec *last,uint32_t next,void *end,struct dm_target_spec **spec,char **target_params) { +static_assert (_Alignof(struct dm_target_spec ) <=8 , +"struct dm_target_spec has excessive alignment requirements" ); +if (next %8 ) { +DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next); +return -EINVAL ; + } + *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 );Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things LabHi
Quoted 4 lines
Some architectures (such as 32-bit x86) specify that the alignment of 64-bit integers is only 4-byte. This could in theory break old userspace code that only uses 4-byte alignment. I would change "next % 8" to "next %
__alignof__(struct dm_target_spec)".
That’s fine, provided that the rest of the code is okay with 4-byte alignment.
Quoted 4 lines
I think that there is no need to backport this patch series to the stable kernels because the bugs that it fixes may only be exploited by the user with
CAP_SYS_ADMINprivilege. So, there is no security or reliability problem being fixed.
I agree that there is no reliability problem, but with kernel lockdown root → kernel is a security boundary, so fixes for memory unsafety problems should still be backported IMO.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v2 2/6 device-mapper: Avoid pointer arithmetic overflow
In reply to Demi Marie ObenourEspecially on 32-bit systems, it is possible for the pointer arithmetic to overflow and cause a userspace pointer to be dereferenced in the kernel.
--- drivers/md/dm-ioctl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/md/dm-ioctl.c +19 -0
@@ -1396,6 +1396,25 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, {static_assert (_Alignof(struct dm_target_spec ) <=8 ,"struct dm_target_spec has excessive alignment requirements" ); +static_assert (offsetof(struct dm_ioctl , data) >=sizeof (struct dm_target_spec ), +"struct dm_target_spec too big" ); + +/* +* Number of bytes remaining, starting with last. This is always +* sizeof(struct dm_target_spec) or more, as otherwise *last was +* out of bounds already. +*/ +size_t remaining = (char *)end - (char *)last; + +/* +* There must be room for both the next target spec and the +* NUL-terminator of the target itself. +*/ +if (remaining -sizeof (struct dm_target_spec ) <= next) { +DMERR ("Target spec extends beyond end of parameters" ); +return -EINVAL ; + } +if (next %8 ) {DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next);return -EINVAL ;
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Re: [dm-devel] [PATCH v2 2/6] device-mapper: Avoid pointer arithmetic overflow
Quoted 7 lines · Demi Marie Obenour
Especially on 32-bit systems, it is possible for the pointer arithmetic to overflow and cause a userspace pointer to be dereferenced in the kernel.
Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Quoted 44 lines
--- drivers/md/dm-ioctl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)drivers/md/dm-ioctl.c +19
-0@@ -1396,6 +1396,25 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, {static_assert (_Alignof(struct dm_target_spec ) <=8 ,"struct dm_target_spec has excessive alignment requirements" ); +static_assert (offsetof(struct dm_ioctl , data) >=sizeof (struct dm_target_spec ), +"struct dm_target_spec too big" ); + +/* +* Number of bytes remaining, starting with last. This is always +* sizeof(struct dm_target_spec) or more, as otherwise *last was +* out of bounds already. +*/ +size_t remaining = (char *)end - (char *)last; + +/* +* There must be room for both the next target spec and the +* NUL-terminator of the target itself. +*/ +if (remaining -sizeof (struct dm_target_spec ) <= next) { +DMERR ("Target spec extends beyond end of parameters" ); +return -EINVAL ; + } +if (next %8 ) {DMERR ("Next target spec (offset %u) is not 8-byte aligned" , next);return -EINVAL ;Signature · 8 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mike Snitzer
maintainerRe: [PATCH v2 2/6] device-mapper: Avoid pointer arithmetic overflow
In reply to Demi Marie ObenourQuoted 16 lines · Demi Marie Obenour
Especially on 32-bit systems, it is possible for the pointer arithmetic to overflow and cause a userspace pointer to be dereferenced in the kernel.
Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")--- drivers/md/dm-ioctl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)drivers/md/dm-ioctl.c +0
-0
drivers/md/dm-ioctl.c static int next_target(struct dm_target_spec *last, uint32_t next, void *end, · line 1400
Quoted 5 lines
drivers/md/dm-ioctl.c +2
-0@@ -1396,6 +1396,25 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, {static_assert (_Alignof(struct dm_target_spec ) <=8 ,"struct dm_target_spec has excessive alignment requirements" ); +static_assert (offsetof(struct dm_ioctl , data) >=sizeof (struct dm_target_spec ), +"struct dm_target_spec too big" );
I'm struggling to see the point for this compile-time check? Especially when you consider (on x86_64):
sizeof(struct dm_target_spec) = 40offsetof(struct dm_ioctl, data) = 305
Just feels like there is no utility offered by adding this check.
SO I've dropped it. But if you feel there is some inherent value please let me know.
Thanks,
Mike
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v2 3/6 device-mapper: structs and parameter strings must not overlap
In reply to Demi Marie ObenourThe NUL terminator for each target parameter string must precede the following 'struct dm_target_spec'. Otherwise, dm_split_args() might corrupt this struct. Furthermore, the first 'struct dm_target_spec' must come after the 'struct dm_ioctl', as if it overlaps too much dm_split_args() could corrupt the 'struct dm_ioctl'.
--- drivers/md/dm-ioctl.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/md/dm-ioctl.c +21 -7
@@ -1391,7 +1391,7 @@ static inline fmode_t get_mode(struct dm_ioctl *param)return mode; } -static int next_target (struct dm_target_spec *last,uint32_t next,void *end, +static int next_target (struct dm_target_spec *last,uint32_t next,const char *end,struct dm_target_spec **spec,char **target_params) { static_assert(_Alignof(struct dm_target_spec) <= 8,
@@ -1404,7 +1404,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end,* sizeof(struct dm_target_spec) or more, as otherwise *last was * out of bounds already. */ -size_t remaining = (char *)end - (char *)last; +size_t remaining = end - (char *)last;/* * There must be room for both the next target spec and the
@@ -1423,10 +1423,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 ); -if (*spec < (last +1 )) -return -EINVAL ; - -return invalid_str (*target_params, end); +return 0 ; } static int populate_table(struct dm_table *table,
@@ -1436,8 +1433,9 @@ static int populate_table(struct dm_table *table,unsigned int i =0 ;struct dm_target_spec *spec = (struct dm_target_spec *) param;uint32_t next = param->data_start; -void *end = (void *) param + param_size; +const char *const end = (const char *) param + param_size;char *target_params; +size_t min_size =sizeof (struct dm_ioctl );if (!param->target_count) {DMERR ("%s: no targets specified" , __func__);
@@ -1445,6 +1443,13 @@ static int populate_table(struct dm_table *table, }for (i =0 ; i < param->target_count; i++) { +const char *nul_terminator; + +if (next < min_size) { +DMERR ("%s: next target spec (offset %u) overlaps %s" , + __func__, next, i ?"previous target" :"'struct dm_ioctl'" ); +return -EINVAL ; + } r =next_target (spec, next, end, &spec, &target_params);if (r) {
@@ -1452,6 +1457,15 @@ static int populate_table(struct dm_table *table,return r; } + nul_terminator =memchr (target_params,0 , (size_t )(end - target_params)); +if (nul_terminator ==NULL ) { +DMERR ("%s: target parameters not NUL-terminated" , __func__); +return -EINVAL ; + } + +/* Add 1 for NUL terminator */ + min_size = (size_t )(nul_terminator - (const char *)spec) +1 ; + r = dm_table_add_target(table, spec->target_type, (sector_t) spec->sector_start, (sector_t) spec->length,
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Re: [dm-devel] [PATCH v2 3/6] device-mapper: structs and parameter strings must not overlap
Quoted 9 lines · Demi Marie Obenour
The NUL terminator for each target parameter string must precede the following 'struct
dm_target_spec'. Otherwise,dm_split_args()might corrupt this struct. Furthermore, the first 'structdm_target_spec' must come after the 'structdm_ioctl', as if it overlaps too muchdm_split_args()could corrupt the 'structdm_ioctl'.Signed-off-by Demi Marie Obenour <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Quoted 89 lines
--- drivers/md/dm-ioctl.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)drivers/md/dm-ioctl.c +21
-7@@ -1391,7 +1391,7 @@ static inline fmode_t get_mode(struct dm_ioctl *param)return mode; } -static int next_target (struct dm_target_spec *last,uint32_t next,void *end, +static int next_target (struct dm_target_spec *last,uint32_t next,const char *end,struct dm_target_spec **spec,char **target_params) { static_assert(_Alignof(struct dm_target_spec) <= 8,@@ -1404,7 +1404,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end,* sizeof(struct dm_target_spec) or more, as otherwise *last was * out of bounds already. */ -size_t remaining = (char *)end - (char *)last; +size_t remaining = end - (char *)last;/* * There must be room for both the next target spec and the @@ -1423,10 +1423,7 @@ static int next_target(struct dm_target_spec *last, uint32_t next, void *end, *spec = (struct dm_target_spec *) ((unsigned char *) last + next); *target_params = (char *) (*spec +1 ); -if (*spec < (last +1 )) -return -EINVAL ; - -return invalid_str (*target_params, end); +return 0 ; } static int populate_table(struct dm_table *table,@@ -1436,8 +1433,9 @@ static int populate_table(struct dm_table *table,unsigned int i =0 ;struct dm_target_spec *spec = (struct dm_target_spec *) param;uint32_t next = param->data_start; -void *end = (void *) param + param_size; +const char *const end = (const char *) param + param_size;char *target_params; +size_t min_size =sizeof (struct dm_ioctl );if (!param->target_count) {DMERR ("%s: no targets specified" , __func__);@@ -1445,6 +1443,13 @@ static int populate_table(struct dm_table *table, }for (i =0 ; i < param->target_count; i++) { +const char *nul_terminator; + +if (next < min_size) { +DMERR ("%s: next target spec (offset %u) overlaps %s" , + __func__, next, i ?"previous target" :"'struct dm_ioctl'" ); +return -EINVAL ; + } r =next_target (spec, next, end, &spec, &target_params);if (r) {@@ -1452,6 +1457,15 @@ static int populate_table(struct dm_table *table,return r; } + nul_terminator =memchr (target_params,0 , (size_t )(end - target_params)); +if (nul_terminator ==NULL ) { +DMERR ("%s: target parameters not NUL-terminated" , __func__); +return -EINVAL ; + } + +/* Add 1 for NUL terminator */ + min_size = (size_t )(nul_terminator - (const char *)spec) +1 ; + r = dm_table_add_target(table, spec->target_type, (sector_t) spec->sector_start, (sector_t) spec->length,Signature · 8 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v2 4/6 device-mapper: Avoid double-fetch of version
In reply to Demi Marie ObenourThe version is fetched once in check_version(), which then does some validation and then overwrites the version in userspace with the API version supported by the kernel. copy_params() then fetches the version from userspace *again*, and this time no validation is done. The result is that the kernel's version number is completely controllable by userspace, provided that userspace can win a race condition.
Fix this flaw by not copying the version back to the kernel the second time. This is not exploitable as the version is not further used in the kernel. However, it could become a problem if future patches start relying on the version field.
--- drivers/md/dm-ioctl.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/md/dm-ioctl.c +18 -12
@@ -1873,30 +1873,33 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)* As well as checking the version compatibility this always * copies the kernel interface version out. */ -static int check_version (unsigned int cmd,struct dm_ioctl __user *user) +static int check_version (unsigned int cmd,struct dm_ioctl __user *user, +struct dm_ioctl *kernel_params) { -uint32_t version[3 ];int r =0 ; -if (copy_from_user (version, user->version,sizeof (version))) +if (copy_from_user (kernel_params->version, user->version,sizeof (kernel_params->version)))return -EFAULT ; -if ((version[0 ] !=DM_VERSION_MAJOR ) || - (version[1 ] >DM_VERSION_MINOR )) { +if ((kernel_params->version[0 ] !=DM_VERSION_MAJOR ) || + (kernel_params->version[1 ] >DM_VERSION_MINOR )) {DMERR ("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)" ,DM_VERSION_MAJOR ,DM_VERSION_MINOR ,DM_VERSION_PATCHLEVEL , - version[0 ], version[1 ], version[2 ], cmd); + kernel_params->version[0 ], + kernel_params->version[1 ], + kernel_params->version[2 ], + cmd); r = -EINVAL ; }/* * Fill in the kernel version. */ - version[0 ] =DM_VERSION_MAJOR ; - version[1 ] =DM_VERSION_MINOR ; - version[2 ] =DM_VERSION_PATCHLEVEL ; -if (copy_to_user (user->version, version,sizeof (version))) + kernel_params->version[0 ] =DM_VERSION_MAJOR ; + kernel_params->version[1 ] =DM_VERSION_MINOR ; + kernel_params->version[2 ] =DM_VERSION_PATCHLEVEL ; +if (copy_to_user (user->version, kernel_params->version,sizeof (kernel_params->version)))return -EFAULT ;return r;
@@ -1922,7 +1925,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -if (copy_from_user (param_kernel, user, minimum_data_size)) +/* Version has been copied from userspace already, avoid TOCTOU */ +if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), + (char __user *)user +sizeof (param_kernel->version), + minimum_data_size -sizeof (param_kernel->version)))return -EFAULT ;if (param_kernel->data_size < minimum_data_size) {
@@ -2034,7 +2040,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us* Check the interface version passed in. This also * writes out the kernel's interface version. */ - r =check_version (cmd, user); + r =check_version (cmd, user, ¶m_kernel);if (r)return r;
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Mike Snitzer
maintainerRe: [PATCH v2 4/6] device-mapper: Avoid double-fetch of version
Quoted 80 lines · Demi Marie Obenour
The version is fetched once in
check_version(), which then does some validation and then overwrites the version in userspace with the API version supported by the kernel.copy_params()then fetches the version from userspace *again*, and this time no validation is done. The result is that the kernel's version number is completely controllable by userspace, provided that userspace can win a race condition.Fix this flaw by not copying the version back to the kernel the second time. This is not exploitable as the version is not further used in the kernel. However, it could become a problem if future patches start relying on the version field.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Demi Marie Obenour <[email protected]>--- drivers/md/dm-ioctl.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)drivers/md/dm-ioctl.c +17
-11@@ -1873,30 +1873,33 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)* As well as checking the version compatibility this always * copies the kernel interface version out. */ -static int check_version (unsigned int cmd,struct dm_ioctl __user *user) +static int check_version (unsigned int cmd,struct dm_ioctl __user *user, +struct dm_ioctl *kernel_params) { -uint32_t version[3 ];int r =0 ; -if (copy_from_user (version, user->version,sizeof (version))) +if (copy_from_user (kernel_params->version, user->version,sizeof (kernel_params->version)))return -EFAULT ; -if ((version[0 ] !=DM_VERSION_MAJOR ) || - (version[1 ] >DM_VERSION_MINOR )) { +if ((kernel_params->version[0 ] !=DM_VERSION_MAJOR ) || + (kernel_params->version[1 ] >DM_VERSION_MINOR )) {DMERR ("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)" ,DM_VERSION_MAJOR ,DM_VERSION_MINOR ,DM_VERSION_PATCHLEVEL , - version[0 ], version[1 ], version[2 ], cmd); + kernel_params->version[0 ], + kernel_params->version[1 ], + kernel_params->version[2 ], + cmd); r = -EINVAL ; }/* * Fill in the kernel version. */ - version[0 ] =DM_VERSION_MAJOR ; - version[1 ] =DM_VERSION_MINOR ; - version[2 ] =DM_VERSION_PATCHLEVEL ; -if (copy_to_user (user->version, version,sizeof (version))) + kernel_params->version[0 ] =DM_VERSION_MAJOR ; + kernel_params->version[1 ] =DM_VERSION_MINOR ; + kernel_params->version[2 ] =DM_VERSION_PATCHLEVEL ; +if (copy_to_user (user->version, kernel_params->version,sizeof (kernel_params->version)))return -EFAULT ;return r;@@ -1922,7 +1925,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -if (copy_from_user (param_kernel, user, minimum_data_size)) +/* Version has been copied from userspace already, avoid TOCTOU */ +if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), + (char __user *)user +sizeof (param_kernel->version), + minimum_data_size -sizeof (param_kernel->version)))return -EFAULT ;if (param_kernel->data_size < minimum_data_size) {
drivers/md/dm-ioctl.c static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us · line 2046
Quoted 8 lines
drivers/md/dm-ioctl.c +1
-1@@ -2034,7 +2040,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us* Check the interface version passed in. This also * writes out the kernel's interface version. */ - r =check_version (cmd, user); + r =check_version (cmd, user, ¶m_kernel);if (r)return r;
I picked this patch up for 6.5, please see:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-6.5&id=c71878e9982075eab2e9f6dc5a09ba7b60ac1e24
But FYI, I folded these changes in:
drivers/md/dm-ioctl.c +4 -1
@@ -1838,6 +1838,9 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user, {int r =0 ; +/* Make certain version is first member of dm_ioctl struct */ +BUILD_BUG_ON (offsetof(struct dm_ioctl , version) !=0 ); +if (copy_from_user (kernel_params->version, user->version,sizeof (kernel_params->version)))return -EFAULT ;
@@ -1885,7 +1888,7 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -/* Version has been copied from userspace already, avoid TOCTOU */ +/* check_version() already copied version from userspace, avoid TOCTOU */ if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), (char __user *)user +sizeof (param_kernel->version), minimum_data_size -sizeof (param_kernel->version)))
Re: [PATCH v2 4/6] device-mapper: Avoid double-fetch of version
Quoted 81 lines · Mike Snitzer
Quoted 78 lines · Demi Marie Obenour
The version is fetched once in
check_version(), which then does some validation and then overwrites the version in userspace with the API version supported by the kernel.copy_params()then fetches the version from userspace *again*, and this time no validation is done. The result is that the kernel's version number is completely controllable by userspace, provided that userspace can win a race condition.Fix this flaw by not copying the version back to the kernel the second time. This is not exploitable as the version is not further used in the kernel. However, it could become a problem if future patches start relying on the version field.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Demi Marie Obenour <[email protected]>--- drivers/md/dm-ioctl.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)drivers/md/dm-ioctl.c +17
-11@@ -1873,30 +1873,33 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)* As well as checking the version compatibility this always * copies the kernel interface version out. */ -static int check_version (unsigned int cmd,struct dm_ioctl __user *user) +static int check_version (unsigned int cmd,struct dm_ioctl __user *user, +struct dm_ioctl *kernel_params) { -uint32_t version[3 ];int r =0 ; -if (copy_from_user (version, user->version,sizeof (version))) +if (copy_from_user (kernel_params->version, user->version,sizeof (kernel_params->version)))return -EFAULT ; -if ((version[0 ] !=DM_VERSION_MAJOR ) || - (version[1 ] >DM_VERSION_MINOR )) { +if ((kernel_params->version[0 ] !=DM_VERSION_MAJOR ) || + (kernel_params->version[1 ] >DM_VERSION_MINOR )) {DMERR ("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)" ,DM_VERSION_MAJOR ,DM_VERSION_MINOR ,DM_VERSION_PATCHLEVEL , - version[0 ], version[1 ], version[2 ], cmd); + kernel_params->version[0 ], + kernel_params->version[1 ], + kernel_params->version[2 ], + cmd); r = -EINVAL ; }/* * Fill in the kernel version. */ - version[0 ] =DM_VERSION_MAJOR ; - version[1 ] =DM_VERSION_MINOR ; - version[2 ] =DM_VERSION_PATCHLEVEL ; -if (copy_to_user (user->version, version,sizeof (version))) + kernel_params->version[0 ] =DM_VERSION_MAJOR ; + kernel_params->version[1 ] =DM_VERSION_MINOR ; + kernel_params->version[2 ] =DM_VERSION_PATCHLEVEL ; +if (copy_to_user (user->version, kernel_params->version,sizeof (kernel_params->version)))return -EFAULT ;return r;@@ -1922,7 +1925,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -if (copy_from_user (param_kernel, user, minimum_data_size)) +/* Version has been copied from userspace already, avoid TOCTOU */ +if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), + (char __user *)user +sizeof (param_kernel->version), + minimum_data_size -sizeof (param_kernel->version)))return -EFAULT ;
Quoted 13 lines
drivers/md/dm-ioctl.c static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us · line 2046
Quoted 10 lines
drivers/md/dm-ioctl.c +1
-1if (param_kernel->data_size < minimum_data_size) {@@ -2034,7 +2040,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us* Check the interface version passed in. This also * writes out the kernel's interface version. */ - r =check_version (cmd, user); + r =check_version (cmd, user, ¶m_kernel);if (r)return r;I picked this patch up for 6.5, please see:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-6.5&id=c71878e9982075eab2e9f6dc5a09ba7b60ac1e24
That’s great, thanks!
Quoted 17 lines
But FYI, I folded these changes in:
drivers/md/dm-ioctl.c +3
-0@@ -1838,6 +1838,9 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user, {int r =0 ; +/* Make certain version is first member of dm_ioctl struct */ +BUILD_BUG_ON (offsetof(struct dm_ioctl , version) !=0 ); +if (copy_from_user (kernel_params->version, user->version,sizeof (kernel_params->version)))return -EFAULT ;
drivers/md/dm-ioctl.c static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kern · line 1894
Quoted 8 lines
drivers/md/dm-ioctl.c +1
-1@@ -1885,7 +1888,7 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernconst size_t minimum_data_size = offsetof(struct dm_ioctl , data);unsigned int noio_flag; -/* Version has been copied from userspace already, avoid TOCTOU */ +/* check_version() already copied version from userspace, avoid TOCTOU */ if (copy_from_user ((char *)param_kernel +sizeof (param_kernel->version), (char __user *)user +sizeof (param_kernel->version), minimum_data_size -sizeof (param_kernel->version)))
Those changes indeed make the code better, thanks for including them!
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v2 5/6 device-mapper: Refuse to create device named "control"
In reply to Demi Marie ObenourTypical userspace setups create a symlink under /dev/mapper with the name of the device, but /dev/mapper/control is reserved for the control device. Therefore, trying to create such a device is almost certain to be a userspace bug.
--- drivers/md/dm-ioctl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/md/dm-ioctl.c +6 -1
@@ -767,7 +767,12 @@ static int get_target_version(struct file *filp, struct dm_ioctl *param, size_tstatic int check_name (const char *name) {if (strchr (name,'/' )) { -DMERR ("invalid device name" ); +DMERR ("device name cannot contain '/'" ); +return -EINVAL ; + } + +if (strcmp (name,DM_CONTROL_NODE ) ==0 ) { +DMERR ("device name cannot be \"%s\"" ,DM_CONTROL_NODE );return -EINVAL ; }
Signature · 8 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v2 6/6 device-mapper: "." and ".." are not valid symlink names
In reply to Demi Marie ObenourUsing either of these is going to greatly confuse userspace, as they are not valid symlink names and so creating the usual /dev/mapper/NAME symlink will not be possible. As creating a device with either of these names is almost certainly a userspace bug, just error out.
--- drivers/md/dm-ioctl.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/dm-ioctl.c +4 -2
@@ -771,8 +771,10 @@ static int check_name(const char *name)return -EINVAL ; } -if (strcmp (name,DM_CONTROL_NODE ) ==0 ) { -DMERR ("device name cannot be \"%s\"" ,DM_CONTROL_NODE ); +if (strcmp (name,DM_CONTROL_NODE ) ==0 || +strcmp (name,"." ) ==0 || +strcmp (name,".." ) ==0 ) { +DMERR ("device name cannot be \"%s\", \".\", or \"..\"" ,DM_CONTROL_NODE );return -EINVAL ; }
Signature · 8 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel