Skip to contentSkip to conversation

ceph: ceph: fix out-of-bound array access when doing a file read

RFC PATCH In review Luis Henriques (SUSE) 4 participants 17 messages Sep 5, 2024 – Dec 11, 2024 Ceph client

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 ceph: ceph: fix out-of-bound array access when doing a file read

__ceph_sync_read() does not correctly handle reads when the inode size is zero. It is easy to hit a NULL pointer dereference by continuously reading a file while, on another client, we keep truncating and writing new data into it.

The NULL pointer dereference happens when the inode size is zero but the read op returns some data (ceph_osdc_wait_request()). This will lead to 'left' being set to a huge value due to the overflow in:

	left = i_size - off;

and, in the loop that follows, the pages[] array being accessed beyond num_pages.

This patch fixes the issue simply by checking the inode size and returning if it is zero, even if there was data from the read op.

Fixes 1065da21e5df ("ceph: stop copying to iter at EOF on sync reads")
---
 fs/ceph/file.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/ceph/file.c +4 -1
index 4b8d59ebda00..41d4eac128bb 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 	if (ceph_inode_is_shutdown(inode))
 		return -EIO;
 
-	if (!len)
+	if (!len || !i_size)
 		return 0;
 	/*
 	 * flush any page cache pages in this range.  this
@@ -1154,6 +1154,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
 		      ret, i_size, (more ? " MORE" : ""));
 
+		if (i_size == 0)
+			ret = 0;
+
 		/* Fix it to go to end of extent map */
 		if (sparse && ret >= 0)
 			ret = ceph_sparse_ext_map_end(op);
#1 lore raw
Quoted 39 lines · Luis Henriques (SUSE)

__ceph_sync_read() does not correctly handle reads when the inode size is zero. It is easy to hit a NULL pointer dereference by continuously reading a file while, on another client, we keep truncating and writing new data into it.

The NULL pointer dereference happens when the inode size is zero but the read op returns some data (ceph_osdc_wait_request()). This will lead to 'left' being set to a huge value due to the overflow in:

	left = i_size - off;

and, in the loop that follows, the pages[] array being accessed beyond num_pages.

This patch fixes the issue simply by checking the inode size and returning if it is zero, even if there was data from the read op.

Fixes 1065da21e5df ("ceph: stop copying to iter at EOF on sync reads")
---
  fs/ceph/file.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/ceph/file.c +1 -1
index 4b8d59ebda00..41d4eac128bb 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
  	if (ceph_inode_is_shutdown(inode))
  		return -EIO;
 
-	if (!len)
+	if (!len || !i_size)
  		return 0;
  	/*
  	 * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1162

Quoted 9 lines
fs/ceph/file.c +3 -0
@@ -1154,6 +1154,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
  		doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
  		      ret, i_size, (more ? " MORE" : ""));
 
+		if (i_size == 0)
+			ret = 0;
+
  		/* Fix it to go to end of extent map */
  		if (sparse && ret >= 0)
  			ret = ceph_sparse_ext_map_end(op);

Hi Luis,

BTW, so in the following code:

1202                 idx = 0;
1203                 if (ret <= 0)
1204                         left = 0;
1205                 else if (off + ret > i_size)
1206                         left = i_size - off;
1207                 else
1208                         left = ret;

The 'ret' should be larger than '0', right ?

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Thanks

- Xiubo

#2 lore raw
Quoted 59 lines · Xiubo Li
Quoted 38 lines · Luis Henriques (SUSE)

__ceph_sync_read() does not correctly handle reads when the inode size is zero. It is easy to hit a NULL pointer dereference by continuously reading a file while, on another client, we keep truncating and writing new data into it.

The NULL pointer dereference happens when the inode size is zero but the read op returns some data (ceph_osdc_wait_request()). This will lead to 'left' being set to a huge value due to the overflow in:

	left = i_size - off;

and, in the loop that follows, the pages[] array being accessed beyond num_pages.

This patch fixes the issue simply by checking the inode size and returning if it is zero, even if there was data from the read op.

Fixes 1065da21e5df ("ceph: stop copying to iter at EOF on sync reads")
---
  fs/ceph/file.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/ceph/file.c +1 -0
index 4b8d59ebda00..41d4eac128bb 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
  	if (ceph_inode_is_shutdown(inode))
  		return -EIO;
  -	if (!len)
+	if (!len || !i_size)
  		return 0;
  	/*
  	 * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1161

Quoted 8 lines
fs/ceph/file.c +2 -0
@@ -1154,6 +1154,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
  		doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
  		      ret, i_size, (more ? " MORE" : ""));
  +		if (i_size == 0)
+			ret = 0;
+
  		/* Fix it to go to end of extent map */
  		if (sparse && ret >= 0)
  			ret = ceph_sparse_ext_map_end(op);

Hi Luis,

BTW, so in the following code:

1202                 idx = 0;
1203                 if (ret <= 0)
1204                         left = 0;
1205                 else if (off + ret > i_size)
1206                         left = i_size - off;
1207                 else
1208                         left = ret;

Quoted 1 line

The 'ret' should be larger than '0', right ?

Right. (Which means we read something from the file.)

Quoted 1 line

If so we do not check anf fix it in the 'else if' branch instead?

Yes, and then we'll have:

	left = i_size - off;

and because 'i_size' is 0, so 'left' will be set to 0xffffffffff... And the loop that follows:

	while (left > 0) {
        	...
        }

will keep looping until we get a NULL pointer. Have you tried the reproducer?

Cheers,

Signature · 1 line
Luís
Quoted 9 lines

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Thanks

- Xiubo

#3 lore raw
Quoted 72 lines · Luis Henriques
Quoted 59 lines · Xiubo Li
Quoted 38 lines · Luis Henriques (SUSE)

__ceph_sync_read() does not correctly handle reads when the inode size is zero. It is easy to hit a NULL pointer dereference by continuously reading a file while, on another client, we keep truncating and writing new data into it.

The NULL pointer dereference happens when the inode size is zero but the read op returns some data (ceph_osdc_wait_request()). This will lead to 'left' being set to a huge value due to the overflow in:

	left = i_size - off;

and, in the loop that follows, the pages[] array being accessed beyond num_pages.

This patch fixes the issue simply by checking the inode size and returning if it is zero, even if there was data from the read op.

Fixes 1065da21e5df ("ceph: stop copying to iter at EOF on sync reads")
---
   fs/ceph/file.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
fs/ceph/file.c +1 -0
index 4b8d59ebda00..41d4eac128bb 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
   	if (ceph_inode_is_shutdown(inode))
   		return -EIO;
   -	if (!len)
+	if (!len || !i_size)
   		return 0;
   	/*
   	 * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1161

Quoted 8 lines
fs/ceph/file.c +2 -0
@@ -1154,6 +1154,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
   		doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
   		      ret, i_size, (more ? " MORE" : ""));
   +		if (i_size == 0)
+			ret = 0;
+
   		/* Fix it to go to end of extent map */
   		if (sparse && ret >= 0)
   			ret = ceph_sparse_ext_map_end(op);

Hi Luis,

BTW, so in the following code:

1202                 idx = 0;
1203                 if (ret <= 0)
1204                         left = 0;
1205                 else if (off + ret > i_size)
1206                         left = i_size - off;
1207                 else
1208                         left = ret;

Quoted 1 line

The 'ret' should be larger than '0', right ?

Right. (Which means we read something from the file.)

Quoted 1 line

If so we do not check anf fix it in the 'else if' branch instead?

Yes, and then we'll have:

	left = i_size - off;

and because 'i_size' is 0, so 'left' will be set to 0xffffffffff... And the loop that follows:

Quoted 6 lines
	while (left > 0) {
         	...
         }

will keep looping until we get a NULL pointer. Have you tried the reproducer?

Hi Luis,

Not yet, and recently I haven't get a chance to do that for the reason as you know.

Thanks

- Xiubo

Quoted 1 line

Cheers,

#4 lore raw
Quoted 77 lines · Xiubo Li
Quoted 76 lines · Luis Henriques
Quoted 59 lines · Xiubo Li
Quoted 38 lines · Luis Henriques (SUSE)

__ceph_sync_read() does not correctly handle reads when the inode size is zero. It is easy to hit a NULL pointer dereference by continuously reading a file while, on another client, we keep truncating and writing new data into it.

The NULL pointer dereference happens when the inode size is zero but the read op returns some data (ceph_osdc_wait_request()). This will lead to 'left' being set to a huge value due to the overflow in:

	left = i_size - off;

and, in the loop that follows, the pages[] array being accessed beyond num_pages.

This patch fixes the issue simply by checking the inode size and returning if it is zero, even if there was data from the read op.

Fixes 1065da21e5df ("ceph: stop copying to iter at EOF on sync reads")
---
   fs/ceph/file.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
fs/ceph/file.c +1 -0
index 4b8d59ebda00..41d4eac128bb 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
   	if (ceph_inode_is_shutdown(inode))
   		return -EIO;
   -	if (!len)
+	if (!len || !i_size)
   		return 0;
   	/*
   	 * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1161

Quoted 8 lines
fs/ceph/file.c +2 -0
@@ -1154,6 +1154,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
   		doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
   		      ret, i_size, (more ? " MORE" : ""));
   +		if (i_size == 0)
+			ret = 0;
+
   		/* Fix it to go to end of extent map */
   		if (sparse && ret >= 0)
   			ret = ceph_sparse_ext_map_end(op);

Hi Luis,

BTW, so in the following code:

1202                 idx = 0;
1203                 if (ret <= 0)
1204                         left = 0;
1205                 else if (off + ret > i_size)
1206                         left = i_size - off;
1207                 else
1208                         left = ret;

Quoted 1 line

The 'ret' should be larger than '0', right ?

Right. (Which means we read something from the file.)

Quoted 1 line

If so we do not check anf fix it in the 'else if' branch instead?

Yes, and then we'll have:

	left = i_size - off;

and because 'i_size' is 0, so 'left' will be set to 0xffffffffff... And the loop that follows:

	while (left > 0) {
         	...
         }
Quoted 7 lines
Quoted 2 lines

will keep looping until we get a NULL pointer. Have you tried the reproducer?

Hi Luis,

Not yet, and recently I haven't get a chance to do that for the reason as you know.

Hi Xiubo,

I know you've been busy, but I was wondering if you (or someone else) had a chance to have a look at this. It's pretty easy to reproduce, and it has been seen in production. Any chances of getting some more feedback on this fix?

Cheers,

Signature · 1 line
Luís
#5 lore raw

Hi Xiubo, Hi Ilya,

On Mon, Sep 30 2024, Luis Henriques wrote:
[...]

Quoted 6 lines

Hi Xiubo,

I know you've been busy, but I was wondering if you (or someone else) had a chance to have a look at this. It's pretty easy to reproduce, and it has been seen in production. Any chances of getting some more feedback on this fix?

It has been a while since I first reported this issue. Taking the risk of being "that annoying guy", I'd like to ping you again on this. I've managed to reproduce the issue very easily, and it's also being triggered very frequently in production. Any news?

Cheers,

Signature · 1 line
Luís
#6 lore raw

CC Alex

Hi Luis,

Alex will take over it and help push it recently. I am a bit busy with my new things these days.

BTW, if possible please join 'ceph' workspace's #cephfs slack channel and you could push it faster there ?

Thanks

- Xiubo

Quoted 16 lines · Luis Henriques

Hi Xiubo, Hi Ilya,

On Mon, Sep 30 2024, Luis Henriques wrote:
[...]

Quoted 6 lines

Hi Xiubo,

I know you've been busy, but I was wondering if you (or someone else) had a chance to have a look at this. It's pretty easy to reproduce, and it has been seen in production. Any chances of getting some more feedback on this fix?

It has been a while since I first reported this issue. Taking the risk of being "that annoying guy", I'd like to ping you again on this. I've managed to reproduce the issue very easily, and it's also being triggered very frequently in production. Any news?

Cheers,

#7 lore raw

Hi Xiubo!

Quoted 6 lines · Xiubo Li

CC Alex

Hi Luis,

Alex will take over it and help push it recently. I am a bit busy with my new things these days.

Thanks a lot. I think the difficult bit to understand (for me, at least!) are any MDS side-effects, as you earlier mentioned the filelocking semantics. I'm not sure if/how this patch may cause troubles there.

Quoted 2 lines

BTW, if possible please join 'ceph' workspace's #cephfs slack channel and you could push it faster there ?

I believe that channel is bridged to IRC (OFTC network), where I'm already lurking (nick 'henrix'). And I see you have already ping'ed others there. However, I'm currently on PTO, so my replies there may be asynchronous :-)

Cheers,

Signature · 1 line
Luís
Quoted 23 lines

Thanks

- Xiubo

Quoted 16 lines · Luis Henriques

Hi Xiubo, Hi Ilya,

On Mon, Sep 30 2024, Luis Henriques wrote:
[...]

Quoted 6 lines

Hi Xiubo,

I know you've been busy, but I was wondering if you (or someone else) had a chance to have a look at this. It's pretty easy to reproduce, and it has been seen in production. Any chances of getting some more feedback on this fix?

It has been a while since I first reported this issue. Taking the risk of being "that annoying guy", I'd like to ping you again on this. I've managed to reproduce the issue very easily, and it's also being triggered very frequently in production. Any news?

Cheers,

#8 lore raw
In reply to Xiubo Li

Hi Xiubo,

Quoted 12 lines

BTW, so in the following code:

1202 idx = 0;
1203 if (ret <= 0)
1204 left = 0;
1205 else if (off + ret > i_size)
1206 left = i_size - off;
1207 else
1208 left = ret;

The 'ret' should be larger than '0', right ?

Quoted 7 lines

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Do you think changing left to ssize_t instead of size_t will fix the problem?

fs/ceph/file.c +2 -2
index 4b8d59ebda00..f8955773bdd7 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 	if (ceph_inode_is_shutdown(inode))
 		return -EIO;
 
-	if (!len)
+	if (!len || !i_size)
 		return 0;
 	/*
 	 * flush any page cache pages in this range.  this
@@ -1087,7 +1087,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		size_t page_off;
 		bool more;
 		int idx;
-		size_t left;
+		ssize_t left;
 		struct ceph_osd_req_op *op;
 		u64 read_off = off;
 		u64 read_len = len;
Signature · 1 line
Goldwyn
#9 lore raw

(CC'ing Alex)

Quoted 40 lines · Goldwyn Rodrigues

Hi Xiubo,

Quoted 12 lines

BTW, so in the following code:

1202 idx = 0;
1203 if (ret <= 0)
1204 left = 0;
1205 else if (off + ret > i_size)
1206 left = i_size - off;
1207 else
1208 left = ret;

The 'ret' should be larger than '0', right ?

Quoted 7 lines

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Do you think changing left to ssize_t instead of size_t will fix the problem?

fs/ceph/file.c +1 -1
index 4b8d59ebda00..f8955773bdd7 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 	if (ceph_inode_is_shutdown(inode))
 		return -EIO;
 
-	if (!len)
+	if (!len || !i_size)
 		return 0;
 	/*
 	 * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1093

Quoted 8 lines
fs/ceph/file.c +1 -1
@@ -1087,7 +1087,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
 		size_t page_off;
 		bool more;
 		int idx;
-		size_t left;
+		ssize_t left;
 		struct ceph_osd_req_op *op;
 		u64 read_off = off;
 		u64 read_len = len;

I *think* (although I haven't tested it) that you're patch should work as well. But I also think it's a bit more hacky: the overflow will still be there:

		if (ret <= 0)
			left = 0;
		else if (off + ret > i_size)
			left = i_size - off;
		else
			left = ret;
		while (left > 0) {
			// ...
		}

If 'i_size' is '0', 'left' (which is now signed) will now have a negative value in the 'else if' branch and the loop that follows will not be executed. My version will simply set 'ret' to '0' before this 'if' construct.

So, in my opinion, what needs to be figured out is whether this will cause problems on the MDS side or not. Because on the kernel client, it should be safe to ignore reads to an inode that has size set to '0', even if there's already data available to be read. Eventually, the inode metadata will get updated and by then we can retry the read.

Unfortunately, the MDS continues to be a huge black box for me and the locking code in particular is very tricky. I'd rather defer this for anyone that is familiar with the code.

Cheers,

Signature · 1 line
Luís
maintainer
#10 lore raw

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

fs/ceph/file.c +0 -0
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,

loff_t *ki_pos,

        if (ceph_inode_is_shutdown(inode))
                return -EIO;

- if (!len)
+ if (!len || !i_size)

                return 0;
        /*
         * flush any page cache pages in this range.  this
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
loff_t *ki_pos,
                }

                idx = 0;

- if (ret <= 0)
- left = 0;
- else if (off + ret > i_size)
- left = i_size - off;
+ if (off + ret > i_size)
+ left = (i_size > off) ? i_size - off : 0;
else
- left = ret;
+ left = (ret > 0) ? ret : 0;
+

                while (left > 0) {
                        size_t plen, copied;
Quoted 87 lines · Luis Henriques

(CC'ing Alex)

Quoted 40 lines · Goldwyn Rodrigues

Hi Xiubo,

Quoted 12 lines

BTW, so in the following code:

1202 idx = 0;
1203 if (ret <= 0)
1204 left = 0;
1205 else if (off + ret > i_size)
1206 left = i_size - off;
1207 else
1208 left = ret;

The 'ret' should be larger than '0', right ?

Quoted 7 lines

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Do you think changing left to ssize_t instead of size_t will fix the problem?

fs/ceph/file.c +1 -1
index 4b8d59ebda00..f8955773bdd7 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
      if (ceph_inode_is_shutdown(inode))
              return -EIO;
 
-     if (!len)
+     if (!len || !i_size)
              return 0;
      /*
       * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1093

Quoted 8 lines
fs/ceph/file.c +1 -1
@@ -1087,7 +1087,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
              size_t page_off;
              bool more;
              int idx;
-             size_t left;
+             ssize_t left;
              struct ceph_osd_req_op *op;
              u64 read_off = off;
              u64 read_len = len;

I *think* (although I haven't tested it) that you're patch should work as well. But I also think it's a bit more hacky: the overflow will still be there:

                if (ret <= 0)
                        left = 0;
                else if (off + ret > i_size)
                        left = i_size - off;
                else
                        left = ret;
                while (left > 0) {
                        // ...
                }

If 'i_size' is '0', 'left' (which is now signed) will now have a negative value in the 'else if' branch and the loop that follows will not be executed. My version will simply set 'ret' to '0' before this 'if' construct.

So, in my opinion, what needs to be figured out is whether this will cause problems on the MDS side or not. Because on the kernel client, it should be safe to ignore reads to an inode that has size set to '0', even if there's already data available to be read. Eventually, the inode metadata will get updated and by then we can retry the read.

Unfortunately, the MDS continues to be a huge black box for me and the locking code in particular is very tricky. I'd rather defer this for anyone that is familiar with the code.

Cheers,
--
Luís

#11 lore raw

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 15 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 6 lines
fs/ceph/file.c +0 -2
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Why not simply modify my patch and do:

		if (i_size < off)
			ret = 0;

instead of:

		if (i_size == 0)
			ret = 0;

?

(Again, totally untested!)

Cheers,

Signature · 1 line
Luís
Quoted 100 lines
fs/ceph/file.c +4 -3
-               else if (off + ret > i_size)
-                       left = i_size - off;
+               if (off + ret > i_size)
+                       left = (i_size > off) ? i_size - off : 0;
                else
-                       left = ret;
+                       left = (ret > 0) ? ret : 0;
+
                while (left > 0) {
                        size_t plen, copied;
Quoted 87 lines · Luis Henriques

(CC'ing Alex)

Quoted 40 lines · Goldwyn Rodrigues

Hi Xiubo,

Quoted 12 lines

BTW, so in the following code:

1202 idx = 0;
1203 if (ret <= 0)
1204 left = 0;
1205 else if (off + ret > i_size)
1206 left = i_size - off;
1207 else
1208 left = ret;

The 'ret' should be larger than '0', right ?

Quoted 7 lines

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Do you think changing left to ssize_t instead of size_t will fix the problem?

fs/ceph/file.c +1 -1
index 4b8d59ebda00..f8955773bdd7 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
      if (ceph_inode_is_shutdown(inode))
              return -EIO;
 
-     if (!len)
+     if (!len || !i_size)
              return 0;
      /*
       * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1093

Quoted 8 lines
fs/ceph/file.c +1 -1
@@ -1087,7 +1087,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
              size_t page_off;
              bool more;
              int idx;
-             size_t left;
+             ssize_t left;
              struct ceph_osd_req_op *op;
              u64 read_off = off;
              u64 read_len = len;

I *think* (although I haven't tested it) that you're patch should work as well. But I also think it's a bit more hacky: the overflow will still be there:

                if (ret <= 0)
                        left = 0;
                else if (off + ret > i_size)
                        left = i_size - off;
                else
                        left = ret;
                while (left > 0) {
                        // ...
                }

If 'i_size' is '0', 'left' (which is now signed) will now have a negative value in the 'else if' branch and the loop that follows will not be executed. My version will simply set 'ret' to '0' before this 'if' construct.

So, in my opinion, what needs to be figured out is whether this will cause problems on the MDS side or not. Because on the kernel client, it should be safe to ignore reads to an inode that has size set to '0', even if there's already data available to be read. Eventually, the inode metadata will get updated and by then we can retry the read.

Unfortunately, the MDS continues to be a huge black box for me and the locking code in particular is very tricky. I'd rather defer this for anyone that is familiar with the code.

Cheers,
--
Luís

maintainer
#12 lore raw

I didn't discard it though :).
I folded it into the if statement. I find the if else construct overly verbose and cumbersome.

+ left = (ret > 0) ? ret : 0;

Quoted 164 lines · Luis Henriques

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 15 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 6 lines
fs/ceph/file.c +0 -2
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Why not simply modify my patch and do:

if (i_size < off)
ret = 0;

instead of:

                if (i_size == 0)
                        ret = 0;

?

(Again, totally untested!)

Cheers,
--
Luís

Quoted 100 lines
fs/ceph/file.c +4 -3
-               else if (off + ret > i_size)
-                       left = i_size - off;
+               if (off + ret > i_size)
+                       left = (i_size > off) ? i_size - off : 0;
                else
-                       left = ret;
+                       left = (ret > 0) ? ret : 0;
+
                while (left > 0) {
                        size_t plen, copied;
Quoted 87 lines · Luis Henriques

(CC'ing Alex)

Quoted 40 lines · Goldwyn Rodrigues

Hi Xiubo,

Quoted 12 lines

BTW, so in the following code:

1202 idx = 0;
1203 if (ret <= 0)
1204 left = 0;
1205 else if (off + ret > i_size)
1206 left = i_size - off;
1207 else
1208 left = ret;

The 'ret' should be larger than '0', right ?

Quoted 7 lines

If so we do not check anf fix it in the 'else if' branch instead?

Because currently the read path code won't exit directly and keep retrying to read if it found that the real content length is longer than the local 'i_size'.

Again I am afraid your current fix will break the MIX filelock semantic ?

Do you think changing left to ssize_t instead of size_t will fix the problem?

fs/ceph/file.c +1 -1
index 4b8d59ebda00..f8955773bdd7 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
      if (ceph_inode_is_shutdown(inode))
              return -EIO;
 
-     if (!len)
+     if (!len || !i_size)
              return 0;
      /*
       * flush any page cache pages in this range.  this

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, · line 1093

Quoted 8 lines
fs/ceph/file.c +1 -1
@@ -1087,7 +1087,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
              size_t page_off;
              bool more;
              int idx;
-             size_t left;
+             ssize_t left;
              struct ceph_osd_req_op *op;
              u64 read_off = off;
              u64 read_len = len;

I *think* (although I haven't tested it) that you're patch should work as well. But I also think it's a bit more hacky: the overflow will still be there:

                if (ret <= 0)
                        left = 0;
                else if (off + ret > i_size)
                        left = i_size - off;
                else
                        left = ret;
                while (left > 0) {
                        // ...
                }

If 'i_size' is '0', 'left' (which is now signed) will now have a negative value in the 'else if' branch and the loop that follows will not be executed. My version will simply set 'ret' to '0' before this 'if' construct.

So, in my opinion, what needs to be figured out is whether this will cause problems on the MDS side or not. Because on the kernel client, it should be safe to ignore reads to an inode that has size set to '0', even if there's already data available to be read. Eventually, the inode metadata will get updated and by then we can retry the read.

Unfortunately, the MDS continues to be a huge black box for me and the locking code in particular is very tricky. I'd rather defer this for anyone that is familiar with the code.

Cheers,
--
Luís

#13 lore raw

Hi!

Quoted 47 lines · Alex Markuze
Quoted 38 lines · Luis Henriques

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 18 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c +0 -0
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
Quoted 7 lines

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 3 lines
fs/ceph/file.c +0 -2
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Quoted 6 lines

I didn't discard it though :).
I folded it into the if statement. I find the if else construct overly verbose and cumbersome.

+ left = (ret > 0) ? ret : 0;

Right, but with your patch, if 'ret < 0', we could still hit the first branch instead of that one:

		if (off + ret > i_size)
			left = (i_size > off) ? i_size - off : 0;
		else
			left = (ret > 0) ? ret : 0;

Cheers,

Signature · 1 line
Luís
maintainer
#14 lore raw

Good catch, I'm reworking the ergonomics of this function, this ret error code is checked and carried through the loop and checked every other line.

Quoted 70 lines · Luis Henriques

Hi!

Quoted 47 lines · Alex Markuze
Quoted 38 lines · Luis Henriques

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 18 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c +0 -0
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
Quoted 7 lines

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 3 lines
fs/ceph/file.c +0 -2
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Quoted 6 lines

I didn't discard it though :).
I folded it into the if statement. I find the if else construct overly verbose and cumbersome.

+ left = (ret > 0) ? ret : 0;

Right, but with your patch, if 'ret < 0', we could still hit the first branch instead of that one:

                if (off + ret > i_size)
                        left = (i_size > off) ? i_size - off : 0;
                else
                        left = (ret > 0) ? ret : 0;

Cheers,
--
Luís

maintainer
#15 lore raw

This patch does three things:

1. The allocated pages are bound to the request, simplifying the memory management especially on the bad path.
2. ret is checked at the earliest point instead of being carried through the loop.
3. The overflow bug is fixed.

fs/ceph/file.c +0 -0
index 4b8d59ebda00..9522d5218c04 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,

loff_t *ki_pos,

        if (ceph_inode_is_shutdown(inode))
                return -EIO;

- if (!len)
+ if (!len || !i_size)

                return 0;
        /*
         * flush any page cache pages in this range.  this
@@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
loff_t *ki_pos,
                int num_pages;
                size_t page_off;
                bool more;
-               int idx;
+               int idx = 0;
                size_t left;
                struct ceph_osd_req_op *op;
                u64 read_off = off;

@@ -1127,7 +1127,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,

osd_req_op_extent_osd_data_pages(req, 0, pages, read_len,
offset_in_page(read_off),
- false, false);
+ false, true);

                op = &req->r_ops[0];
                if (sparse) {
@@ -1160,7 +1160,15 @@ ssize_t __ceph_sync_read(struct inode *inode,
loff_t *ki_pos,
                else if (ret == -ENOENT)
                        ret = 0;

- if (ret > 0 && IS_ENCRYPTED(inode)) {
+ if (ret < 0) {
+ ceph_osdc_put_request(req);
+
+ if (ret == -EBLOCKLISTED)
+ fsc->blocklisted = true;
+ break;
+ }
+
+ if (IS_ENCRYPTED(inode)) {
int fret;

fret = ceph_fscrypt_decrypt_extents(inode, pages,
@@ -1186,10 +1194,8 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,

                        ret = min_t(ssize_t, fret, len);
                }

- ceph_osdc_put_request(req);
-

                /* Short read but not EOF? Zero out the remainder. */
-               if (ret >= 0 && ret < len && (off + ret < i_size)) {
+               if (ret < len && (off + ret < i_size)) {
                        int zlen = min(len - ret, i_size - off - ret);
                        int zoff = page_off + ret;

@@ -1199,13 +1205,11 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,

                        ret += zlen;
                }

- idx = 0;
- if (ret <= 0)
- left = 0;
- else if (off + ret > i_size)
- left = i_size - off;
+ if (off + ret > i_size)

+                       left = (i_size > off) ? i_size - off : 0;
                else
                        left = ret;
+
                while (left > 0) {
                        size_t plen, copied;

@@ -1221,13 +1225,8 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,

                                break;
                        }
                }

- ceph_release_page_vector(pages, num_pages);

- if (ret < 0) {
- if (ret == -EBLOCKLISTED)
- fsc->blocklisted = true;
- break;
- }
+ ceph_osdc_put_request(req);

                if (off >= i_size || !more)
                        break;
Quoted 76 lines · Alex Markuze

Good catch, I'm reworking the ergonomics of this function, this ret error code is checked and carried through the loop and checked every other line.

Quoted 70 lines · Luis Henriques

Hi!

Quoted 47 lines · Alex Markuze
Quoted 38 lines · Luis Henriques

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 18 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c +0 -0
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
Quoted 7 lines

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 3 lines
fs/ceph/file.c +0 -2
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Quoted 6 lines

I didn't discard it though :).
I folded it into the if statement. I find the if else construct overly verbose and cumbersome.

+ left = (ret > 0) ? ret : 0;

Right, but with your patch, if 'ret < 0', we could still hit the first branch instead of that one:

                if (off + ret > i_size)
                        left = (i_size > off) ? i_size - off : 0;
                else
                        left = (ret > 0) ? ret : 0;

Cheers,
--
Luís

maintainer
#16 lore raw

Folks, this is the fix please add your reviews:

https://marc.info/?l=ceph-devel&m=173367895206137&w=2

Quoted 186 lines · Alex Markuze

This patch does three things:

1. The allocated pages are bound to the request, simplifying the memory management especially on the bad path.
2. ret is checked at the earliest point instead of being carried through the loop.
3. The overflow bug is fixed.

fs/ceph/file.c +1 -1
index 4b8d59ebda00..9522d5218c04 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c +1 -1
@@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                int num_pages;
                size_t page_off;
                bool more;
-               int idx;
+               int idx = 0;
                size_t left;
                struct ceph_osd_req_op *op;
                u64 read_off = off;
fs/ceph/file.c +1 -1
@@ -1127,7 +1127,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
 
                osd_req_op_extent_osd_data_pages(req, 0, pages, read_len,
                                                 offset_in_page(read_off),
-                                                false, false);
+                                                false, true);
 
                op = &req->r_ops[0];
                if (sparse) {
fs/ceph/file.c +9 -1
@@ -1160,7 +1160,15 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                else if (ret == -ENOENT)
                        ret = 0;
 
-               if (ret > 0 && IS_ENCRYPTED(inode)) {
+               if (ret < 0) {
+                       ceph_osdc_put_request(req);
+
+                       if (ret == -EBLOCKLISTED)
+                               fsc->blocklisted = true;
+                       break;
+               }
+
+               if (IS_ENCRYPTED(inode)) {
                        int fret;
 

fret = ceph_fscrypt_decrypt_extents(inode, pages,

fs/ceph/file.c +5 -14
@@ -1186,10 +1194,8 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                        ret = min_t(ssize_t, fret, len);
                }
 
-               ceph_osdc_put_request(req);
-
                /* Short read but not EOF? Zero out the remainder. */
-               if (ret >= 0 && ret < len && (off + ret < i_size)) {
+               if (ret < len && (off + ret < i_size)) {
                        int zlen = min(len - ret, i_size - off - ret);
                        int zoff = page_off + ret;
@@ -1199,13 +1205,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                        ret += zlen;
                }
 
-               idx = 0;
-               if (ret <= 0)
-                       left = 0;
-               else if (off + ret > i_size)
-                       left = i_size - off;
+               if (off + ret > i_size)
+                       left = (i_size > off) ? i_size - off : 0;
                else
                        left = ret;
+
                while (left > 0) {
                        size_t plen, copied;
@@ -1221,13 +1225,8 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                                break;
                        }
                }
-               ceph_release_page_vector(pages, num_pages);
 
-               if (ret < 0) {
-                       if (ret == -EBLOCKLISTED)
-                               fsc->blocklisted = true;
-                       break;
-               }
+               ceph_osdc_put_request(req);
 
                if (off >= i_size || !more)
                        break;
Quoted 76 lines · Alex Markuze

Good catch, I'm reworking the ergonomics of this function, this ret error code is checked and carried through the loop and checked every other line.

Quoted 70 lines · Luis Henriques

Hi!

Quoted 47 lines · Alex Markuze
Quoted 38 lines · Luis Henriques

Hi Alex,

[ Thank you for looking into this. ]

Quoted 9 lines · Alex Markuze

Hi, Folks.
AFAIK there is no side effect that can affect MDS with this fix. This crash happens following this patch "1065da21e5df9d843d2c5165d5d576be000142a6" "ceph: stop copying to iter at EOF on sync reads".

Per your fix Luis, it seems to address only the cases when i_size goes to zero but can happen anytime the i_size goes below off. I propose fixing it this way:

Hmm... you're probably right. I didn't see this happening, but I guess it could indeed happen.

Quoted 18 lines
fs/ceph/file.c +1 -1
index 4b8d59ebda00..19b084212fee 100644
@@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
        if (ceph_inode_is_shutdown(inode))
                return -EIO;
 
-       if (!len)
+       if (!len || !i_size)
                return 0;
        /*

* flush any page cache pages in this range. this

fs/ceph/file.c +0 -0
@@ -1200,12 +1200,11 @@ ssize_t __ceph_sync_read(struct inode *inode,
 loff_t *ki_pos,
                }
 
Quoted 7 lines

fs/ceph/file.c ssize_t __ceph_sync_read(struct inode *inode, · line 1205

Quoted 3 lines
fs/ceph/file.c +0 -2
                idx = 0;
-               if (ret <= 0)
-                       left = 0;

Right now I don't have any means for testing this patch. However, I don't think this is completely correct. By removing the above condition you're discarding cases where an error has occurred (i.e. where ret is negative).

Quoted 6 lines

I didn't discard it though :).
I folded it into the if statement. I find the if else construct overly verbose and cumbersome.

+ left = (ret > 0) ? ret : 0;

Right, but with your patch, if 'ret < 0', we could still hit the first branch instead of that one:

                if (off + ret > i_size)
                        left = (i_size > off) ? i_size - off : 0;
                else
                        left = (ret > 0) ? ret : 0;

Cheers,
--
Luís