Whamcloud - gitweb
LU-6078 utils: fix copytool file bounds checking 26/13226/3
authorBruno Faccini <bruno.faccini@intel.com>
Fri, 2 Jan 2015 12:37:37 +0000 (13:37 +0100)
committerOleg Drokin <oleg.drokin@intel.com>
Wed, 18 Mar 2015 11:10:12 +0000 (11:10 +0000)
Strengthen copytool file bounds checking in either full
or partial/extent mode.

Signed-off-by: Bruno Faccini <bruno.faccini@intel.com>
Change-Id: I36939f9a18362ca3131e39b8d390978dfc79405a
Reviewed-on: http://review.whamcloud.com/13226
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: Henri Doreau <henri.doreau@cea.fr>
Reviewed-by: Hongchao Zhang <hongchao.zhang@intel.com>
Reviewed-by: Oleg Drokin <oleg.drokin@intel.com>
lustre/utils/lhsmtool_posix.c

index db0dd98..5a81e4f 100644 (file)
@@ -543,6 +543,14 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                return rc;
        }
 
                return rc;
        }
 
+       if (hai->hai_extent.offset > (__u64)src_st.st_size) {
+               rc = -EINVAL;
+               CT_ERROR(rc, "Trying to start reading past end ("LPU64" > "
+                        "%jd) of '%s' source file", hai->hai_extent.offset,
+                        (intmax_t)src_st.st_size, src);
+               return rc;
+       }
+
        if (fstat(dst_fd, &dst_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", dst);
        if (fstat(dst_fd, &dst_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", dst);
@@ -555,17 +563,9 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                return rc;
        }
 
                return rc;
        }
 
-       rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
-       if (rc < 0) {
-               rc = -errno;
-               CT_ERROR(rc,
-                        "cannot seek for read to "LPU64" (len %jd) in '%s'",
-                        hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
-               return rc;
-       }
-
        /* Don't read beyond a given extent */
        /* Don't read beyond a given extent */
-       length = min(hai->hai_extent.length, src_st.st_size);
+       length = min(hai->hai_extent.length,
+                    src_st.st_size - hai->hai_extent.offset);
 
        start_time = last_bw_print = last_report_time = time(NULL);
 
 
        start_time = last_bw_print = last_report_time = time(NULL);