diff mbox series

[v2] raw handler: Allow symlink traversal in blkprotect

Message ID 20210106152111.20197-1-wesley.lindauer@gmail.com
State Accepted
Headers show
Series [v2] raw handler: Allow symlink traversal in blkprotect | expand

Commit Message

Wes Lindauer Jan. 6, 2021, 3:21 p.m. UTC
From: Wes Lindauer <wesley.lindauer@gmail.com>

If the device given in the sw-description is a symlink, this will
traverse the symlink to find if the underlying block device has a
force_ro flag.

This is useful when using device symlinks with udev rules. i.e.
/dev/disk/by-label/boot -> /dev/mmcblk0boot0. These udev symlinks can
be helpful when using an A/B partitioning layout.

Signed-off-by: Wes Lindauer <wesley.lindauer@gmail.com>
---
 handlers/raw_handler.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Stefano Babic Jan. 6, 2021, 3:38 p.m. UTC | #1
On 06.01.21 16:21, wesley.lindauer@gmail.com wrote:
> From: Wes Lindauer <wesley.lindauer@gmail.com>
> 
> If the device given in the sw-description is a symlink, this will
> traverse the symlink to find if the underlying block device has a
> force_ro flag.
> 
> This is useful when using device symlinks with udev rules. i.e.
> /dev/disk/by-label/boot -> /dev/mmcblk0boot0. These udev symlinks can
> be helpful when using an A/B partitioning layout.
> 
> Signed-off-by: Wes Lindauer <wesley.lindauer@gmail.com>
> ---
>  handlers/raw_handler.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
> index 489919d..542d942 100644
> --- a/handlers/raw_handler.c
> +++ b/handlers/raw_handler.c
> @@ -38,6 +38,7 @@ void raw_copyimage_handler(void);
>   */
>  static int blkprotect(struct img_type *img, bool on)
>  {
> +	char abs_path[PATH_MAX];
>  	const char c_sys_path[] = "/sys/class/block/%s/force_ro";
>  	const char c_unprot_char = '0';
>  	const char c_prot_char = '1';
> @@ -53,7 +54,7 @@ static int blkprotect(struct img_type *img, bool on)
>  		return ret;
>  	}
>  
> -	if (lstat(img->device, &sb) == -1) {
> +	if (stat(img->device, &sb) == -1) {
>  		TRACE("stat for device %s failed: %s", img->device, strerror(errno));
>  		return ret;
>  	}
> @@ -61,7 +62,13 @@ static int blkprotect(struct img_type *img, bool on)
>  		return ret;
>  	}
>  
> -	ret_int = asprintf(&sysfs_path, c_sys_path, img->device + 5);  // remove "/dev/" from device path
> +	/* If given, traverse symlink and convert to absolute path */
> +	if (realpath(img->device, abs_path) == NULL) {
> +		ret = -errno;
> +		goto blkprotect_out;
> +	}
> +
> +	ret_int = asprintf(&sysfs_path, c_sys_path, abs_path + 5);  /* remove "/dev/" from device path */
>  	if(ret_int < 0) {
>  		ret = -ENOMEM;
>  		goto blkprotect_out;
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index 489919d..542d942 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -38,6 +38,7 @@  void raw_copyimage_handler(void);
  */
 static int blkprotect(struct img_type *img, bool on)
 {
+	char abs_path[PATH_MAX];
 	const char c_sys_path[] = "/sys/class/block/%s/force_ro";
 	const char c_unprot_char = '0';
 	const char c_prot_char = '1';
@@ -53,7 +54,7 @@  static int blkprotect(struct img_type *img, bool on)
 		return ret;
 	}
 
-	if (lstat(img->device, &sb) == -1) {
+	if (stat(img->device, &sb) == -1) {
 		TRACE("stat for device %s failed: %s", img->device, strerror(errno));
 		return ret;
 	}
@@ -61,7 +62,13 @@  static int blkprotect(struct img_type *img, bool on)
 		return ret;
 	}
 
-	ret_int = asprintf(&sysfs_path, c_sys_path, img->device + 5);  // remove "/dev/" from device path
+	/* If given, traverse symlink and convert to absolute path */
+	if (realpath(img->device, abs_path) == NULL) {
+		ret = -errno;
+		goto blkprotect_out;
+	}
+
+	ret_int = asprintf(&sysfs_path, c_sys_path, abs_path + 5);  /* remove "/dev/" from device path */
 	if(ret_int < 0) {
 		ret = -ENOMEM;
 		goto blkprotect_out;