imsm: introduce helpers to manage file descriptors
PATCH 1/1 imsm: introduce helpers to manage file descriptors
To avoid direct comparisions define dedicated inlines.
This patch propagates them in super-intel.c. They are declared globally for future usage outside IMSM.
Additionally, it adds fd check in save_backup_imsm() to remove code vulnerability and simplifies targets array implementation.
It also propagates prr_vrb() macro instead if (verbose) condidtion.
---
Hi Jes,
For now scope is limited to IMSM. If you ok the idea please apply it. I would like to use it in other places, but first I need to have it applied.
mdadm.h | 25 ++++++++ super-intel.c | 168 +++++++++++++++++++++++--------------------------- 2 files changed, 101 insertions(+), 92 deletions(-)
mdadm.h +25 -0
@@ -782,6 +782,31 @@ static inline char *map_dev(int major, int minor, int create)return map_dev_preferred (major, minor, create,NULL ); } +/** +* is_fd_valid() - check file descriptor. +* @fd: file descriptor. +* +* The function checks if @fd is nonnegative integer and shall be used only +* to verify open() result. +*/ +static inline int is_fd_valid (int fd) +{ +return (fd >-1 ); +} + +/** +* close_fd() - verify, close and unset file descriptor. +* @fd: pointer to file descriptor. +* +* The function closes and invalidates file descriptor if appropriative. It +* ignores incorrect file descriptor quitely to simplify error handling. +*/ +static inline void close_fd (int *fd) +{ +if (is_fd_valid (*fd) &&close (*fd) ==0 ) + *fd =-1 ; +} +struct active_array ;struct metadata_update ;
super-intel.c +76 -92
414 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 7 lines · Mariusz Tkaczyk
To avoid direct comparisions define dedicated inlines.
This patch propagates them insuper-intel.c. They are declared globally for future usage outside IMSM.Additionally, it adds fd check in
save_backup_imsm()to remove code vulnerability and simplifies targets array implementation.
Quoted 8 lines
It also propagates
prr_vrb()macro instead if (verbose) condidtion.Signed-off-by Mariusz Tkaczyk <[email protected]>---
Hi Jes,
For now scope is limited to IMSM. If you ok the idea please apply it. I would like to use it in other places, but first I need to have it applied.
Hi Mariusz,
I am not a huge fan of hiding things in wrappers, however we've seen a number of cases in the code with inconsistent checks for > 0 and >= 0, so I guess it makes sense.
However I think you introduce a bug or at least a behavioral change in the code, please see below.
Quoted 17 lines
super-intel.c +3
-7@@ -7643,26 +7635,27 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,/* This device needs to be a device in an 'imsm' container */ fd =open (dev,O_RDONLY |O_EXCL ,0 ); -if (fd >=0 ) { -if (verbose) -pr_err ("Cannot create this array on device %s\n" , - dev); + +if (is_fd_valid (fd)) { +pr_vrb ("Cannot create this array on device %s\n" , dev);close (fd);return 0 ; } -if (errno !=EBUSY || (fd =open (dev,O_RDONLY ,0 )) <0 ) { -if (verbose) - pr_err("Cannot open %s: %s\n",
super-intel.c static int validate_geometry_imsm(struct supertype *st, int level, int layout, · line 7651
Quoted 10 lines
super-intel.c +7
-2- dev, strerror(errno)); -return 0 ; +if (errno !=EBUSY ) { + fd =open (dev,O_RDONLY ,0 ); + +if (!is_fd_valid (fd)) { +pr_vrb ("Cannot open %s: %s\n" , dev,strerror (errno)); +return 0 ; + } }
The old code would only call open(dev, O_RDONLY, 0) if errno == EBUSY, and it enters the if() block unconditionally, unless errno == EBUSY and we fail to open O_RDONLY.
With your change you try to reopen the device for all error cases, except for EBUSY.
I assume this is a mistake? If you do want to change the logic, it would be preferred to do it in a separate patch.
Cheers,
Jes