From 7c293700db4d1b14ba55bc221af9afc0ed233f01 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:11 -0800 Subject: common: Always return enum filetype in file_name_detect_type_offset() None of the callers of file_name_detect_type_offset() are prepared to deal with negative error code. Change the code to return filetype_unknown if open_and_lseek() fails. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/filetype.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/filetype.c b/common/filetype.c index f8b6bc8954..fb029a7739 100644 --- a/common/filetype.c +++ b/common/filetype.c @@ -383,7 +383,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos) fd = open_and_lseek(filename, O_RDONLY, pos); if (fd < 0) - return fd; + goto out; buf = xzalloc(FILE_TYPE_SAFE_BUFSIZE); @@ -396,7 +396,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos) err_out: close(fd); free(buf); - +out: return type; } -- cgit v1.2.3 From eae44ac6322312732a27e57d3cbfb78c5f2e13cc Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:18 -0800 Subject: uimage: Fix lseek error check in uimage_verify() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/uimage.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/uimage.c b/common/uimage.c index 3273bc1871..72b668e898 100644 --- a/common/uimage.c +++ b/common/uimage.c @@ -265,11 +265,12 @@ int uimage_verify(struct uimage_handle *handle) { u32 crc = 0; int len, ret; + loff_t off; void *buf; - ret = lseek(handle->fd, sizeof(struct image_header), SEEK_SET); - if (ret < 0) - return ret; + off = sizeof(struct image_header); + if (lseek(handle->fd, off, SEEK_SET) != off) + return -errno; buf = xmalloc(PAGE_SIZE); -- cgit v1.2.3 From cb4b3d397361e04a5de1da71e84522f6bfba11ba Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:19 -0800 Subject: uimage: Fix lseek error check in uimage_load() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/uimage.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/uimage.c b/common/uimage.c index 72b668e898..12c7e9e2c1 100644 --- a/common/uimage.c +++ b/common/uimage.c @@ -308,6 +308,7 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no, image_header_t *hdr = &handle->header; struct uimage_handle_data *iha; int ret; + loff_t off; int (*uncompress_fn)(unsigned char *inbuf, int len, int(*fill)(void*, unsigned int), int(*flush)(void*, unsigned int), @@ -320,10 +321,9 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no, iha = &handle->ihd[image_no]; - ret = lseek(handle->fd, iha->offset + handle->data_offset, - SEEK_SET); - if (ret < 0) - return ret; + off = iha->offset + handle->data_offset; + if (lseek(handle->fd, off, SEEK_SET) != off) + return -errno; /* if ramdisk U-Boot expect to ignore the compression type */ if (hdr->ih_comp == IH_COMP_NONE || hdr->ih_type == IH_TYPE_RAMDISK) -- cgit v1.2.3 From 7af10336201f9d458a4f7baefec4d0d8a8c60141 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:20 -0800 Subject: uimage: Fix lseek error check in uimage_load_to_buf() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/uimage.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'common') diff --git a/common/uimage.c b/common/uimage.c index 12c7e9e2c1..35bfb10b06 100644 --- a/common/uimage.c +++ b/common/uimage.c @@ -457,6 +457,7 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, { u32 size; int ret; + loff_t off; struct uimage_handle_data *ihd; char ftbuf[128]; enum filetype ft; @@ -467,9 +468,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, ihd = &handle->ihd[image_no]; - ret = lseek(handle->fd, ihd->offset + handle->data_offset, - SEEK_SET); - if (ret < 0) + off = ihd->offset + handle->data_offset; + if (lseek(handle->fd, off, SEEK_SET) != off) return NULL; if (handle->header.ih_comp == IH_COMP_NONE) { @@ -497,10 +497,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, if (ft != filetype_gzip) return NULL; - ret = lseek(handle->fd, ihd->offset + handle->data_offset + - ihd->len - 4, - SEEK_SET); - if (ret < 0) + off = ihd->offset + handle->data_offset + ihd->len - 4; + if (lseek(handle->fd, off, SEEK_SET) != off) return NULL; ret = read(handle->fd, &size, 4); @@ -509,9 +507,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, size = le32_to_cpu(size); - ret = lseek(handle->fd, ihd->offset + handle->data_offset, - SEEK_SET); - if (ret < 0) + off = ihd->offset + handle->data_offset; + if (lseek(handle->fd, off, SEEK_SET) != off) return NULL; buf = malloc(size); -- cgit v1.2.3 From 219b954a11e82afbbd7b6ef13d8c5ba94a5b0ff3 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:21 -0800 Subject: state: Fix lseek error check in state_backend_bucket_direct_read() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/state/backend_bucket_direct.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'common') diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c index 1f00b0fb2f..1524312146 100644 --- a/common/state/backend_bucket_direct.c +++ b/common/state/backend_bucket_direct.c @@ -56,10 +56,9 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket void *buf; int ret; - ret = lseek(direct->fd, direct->offset, SEEK_SET); - if (ret < 0) { - dev_err(direct->dev, "Failed to seek file, %d\n", ret); - return ret; + if (lseek(direct->fd, direct->offset, SEEK_SET) != direct->offset) { + dev_err(direct->dev, "Failed to seek file, %d\n", -errno); + return -errno; } ret = read_full(direct->fd, &meta, sizeof(meta)); if (ret < 0) { @@ -77,10 +76,11 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket return -EINVAL; } read_len = direct->max_size; - ret = lseek(direct->fd, direct->offset, SEEK_SET); - if (ret < 0) { - dev_err(direct->dev, "Failed to seek file, %d\n", ret); - return ret; + if (lseek(direct->fd, direct->offset, SEEK_SET) != + direct->offset) { + dev_err(direct->dev, "Failed to seek file, %d\n", + -errno); + return -errno; } } -- cgit v1.2.3 From ec25ecfbcb47cb83b310b9e177a5b65de3781dec Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:22 -0800 Subject: state: Fix lseek error check in state_backend_bucket_direct_write() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/state/backend_bucket_direct.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c index 1524312146..95ddb93106 100644 --- a/common/state/backend_bucket_direct.c +++ b/common/state/backend_bucket_direct.c @@ -113,10 +113,9 @@ static int state_backend_bucket_direct_write(struct state_backend_storage_bucket if (len > direct->max_size - sizeof(meta)) return -E2BIG; - ret = lseek(direct->fd, direct->offset, SEEK_SET); - if (ret < 0) { - dev_err(direct->dev, "Failed to seek file, %d\n", ret); - return ret; + if (lseek(direct->fd, direct->offset, SEEK_SET) != direct->offset) { + dev_err(direct->dev, "Failed to seek file, %d\n", -errno); + return -errno; } /* write the meta data only if there is head room */ -- cgit v1.2.3 From 8a6a9fbcecffab1b076edfad94d4f32bb2cc9435 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:23 -0800 Subject: state: Fix lseek error check in state_mtd_peb_read() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/state/backend_bucket_circular.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c index da7c8421ae..791f39b9b6 100644 --- a/common/state/backend_bucket_circular.c +++ b/common/state/backend_bucket_circular.c @@ -162,11 +162,10 @@ static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ offset += (off_t)circ->eraseblock * circ->mtd->erasesize; - ret = lseek(circ->fd, offset, SEEK_SET); - if (ret < 0) { + if (lseek(circ->fd, offset, SEEK_SET) != offset) { dev_err(circ->dev, "Failed to set circular read position to %lld, %d\n", - (long long) offset, ret); - return ret; + (long long) offset, -errno); + return -errno; } dev_dbg(circ->dev, "Read state from %lld length %d\n", (long long) offset, -- cgit v1.2.3 From 5eadd11d4795afb6b521b5c3249c6341c0be7117 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 6 Mar 2019 23:49:24 -0800 Subject: state: Fix lseek error check in state_mtd_peb_write() Don't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/state/backend_bucket_circular.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c index 791f39b9b6..4676730d05 100644 --- a/common/state/backend_bucket_circular.c +++ b/common/state/backend_bucket_circular.c @@ -190,11 +190,10 @@ static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *cir offset += circ->eraseblock * circ->mtd->erasesize; - ret = lseek(circ->fd, offset, SEEK_SET); - if (ret < 0) { + if (lseek(circ->fd, offset, SEEK_SET) != offset) { dev_err(circ->dev, "Failed to set position for circular write %lld, %d\n", - (long long) offset, ret); - return ret; + (long long) offset, -errno); + return -errno; } ret = write_full(circ->fd, buf, len); -- cgit v1.2.3