linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] coredump: Ensure proper size of sparse core files
@ 2016-12-08 18:11 Dave Kleikamp
  2017-01-11 19:25 ` [PATCH RESEND] " Dave Kleikamp
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Kleikamp @ 2016-12-08 18:11 UTC (permalink / raw)
  To: Alexander Viro; +Cc: sparclinux, Dave Kleikamp, linux-fsdevel, linux-kernel

If the last section of a core file ends with an unmapped or zero page,
the size of the file does not correspond with the last dump_skip() call.
gdb complains that the file is truncated and can be confusing to users.

After all of the vma sections are written, make sure that the file size
is no smaller than the current file position.

This problem can be demonstrated with gdb's bigcore testcase on the
sparc architecture.

Signed-off-by: Dave Kleikamp <dave.kleikamp@oracle.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 fs/binfmt_elf.c          |  1 +
 fs/coredump.c            | 18 ++++++++++++++++++
 include/linux/coredump.h |  1 +
 3 files changed, 20 insertions(+)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 2472af2..cfd724f 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -2296,6 +2296,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 				goto end_coredump;
 		}
 	}
+	dump_truncate(cprm);
 
 	if (!elf_core_write_extra_data(cprm))
 		goto end_coredump;
diff --git a/fs/coredump.c b/fs/coredump.c
index eb9c92c..ecdb1e3 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -833,3 +833,21 @@ int dump_align(struct coredump_params *cprm, int align)
 	return mod ? dump_skip(cprm, align - mod) : 1;
 }
 EXPORT_SYMBOL(dump_align);
+
+/*
+ * Insures that file size is big enough to contain the current file
+ * postion. This prevents gdb from complaining about a truncated file
+ * if the last "write" to the file was dump_skip.
+ */
+void dump_truncate(struct coredump_params *cprm)
+{
+	struct file *file = cprm->file;
+	loff_t offset;
+
+	if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
+		offset = file->f_op->llseek(file, 0, SEEK_CUR);
+		if (i_size_read(file->f_mapping->host) < offset)
+			do_truncate(file->f_path.dentry, offset, 0, file);
+	}
+}
+EXPORT_SYMBOL(dump_truncate);
diff --git a/include/linux/coredump.h b/include/linux/coredump.h
index d016a12..28ffa94 100644
--- a/include/linux/coredump.h
+++ b/include/linux/coredump.h
@@ -14,6 +14,7 @@ struct coredump_params;
 extern int dump_skip(struct coredump_params *cprm, size_t nr);
 extern int dump_emit(struct coredump_params *cprm, const void *addr, int nr);
 extern int dump_align(struct coredump_params *cprm, int align);
+extern void dump_truncate(struct coredump_params *cprm);
 #ifdef CONFIG_COREDUMP
 extern void do_coredump(const siginfo_t *siginfo);
 #else
-- 
2.10.2

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH RESEND] coredump: Ensure proper size of sparse core files
  2016-12-08 18:11 [PATCH] coredump: Ensure proper size of sparse core files Dave Kleikamp
@ 2017-01-11 19:25 ` Dave Kleikamp
  2017-01-17  3:42   ` David Miller
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Kleikamp @ 2017-01-11 19:25 UTC (permalink / raw)
  To: Alexander Viro; +Cc: sparclinux, linux-fsdevel, linux-kernel

If the last section of a core file ends with an unmapped or zero page,
the size of the file does not correspond with the last dump_skip() call.
gdb complains that the file is truncated and can be confusing to users.

After all of the vma sections are written, make sure that the file size
is no smaller than the current file position.

This problem can be demonstrated with gdb's bigcore testcase on the
sparc architecture.

Signed-off-by: Dave Kleikamp <dave.kleikamp@oracle.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 fs/binfmt_elf.c          |  1 +
 fs/coredump.c            | 18 ++++++++++++++++++
 include/linux/coredump.h |  1 +
 3 files changed, 20 insertions(+)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 29a02daf08a9..422370293cfd 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -2298,6 +2298,7 @@ static int elf_core_dump(struct coredump_params *cprm)
 				goto end_coredump;
 		}
 	}
+	dump_truncate(cprm);
 
 	if (!elf_core_write_extra_data(cprm))
 		goto end_coredump;
diff --git a/fs/coredump.c b/fs/coredump.c
index e525b6017cdf..ae6b05629ca1 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -833,3 +833,21 @@ int dump_align(struct coredump_params *cprm, int align)
 	return mod ? dump_skip(cprm, align - mod) : 1;
 }
 EXPORT_SYMBOL(dump_align);
+
+/*
+ * Ensures that file size is big enough to contain the current file
+ * postion. This prevents gdb from complaining about a truncated file
+ * if the last "write" to the file was dump_skip.
+ */
+void dump_truncate(struct coredump_params *cprm)
+{
+	struct file *file = cprm->file;
+	loff_t offset;
+
+	if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
+		offset = file->f_op->llseek(file, 0, SEEK_CUR);
+		if (i_size_read(file->f_mapping->host) < offset)
+			do_truncate(file->f_path.dentry, offset, 0, file);
+	}
+}
+EXPORT_SYMBOL(dump_truncate);
diff --git a/include/linux/coredump.h b/include/linux/coredump.h
index d016a121a8c4..28ffa94aed6b 100644
--- a/include/linux/coredump.h
+++ b/include/linux/coredump.h
@@ -14,6 +14,7 @@ struct coredump_params;
 extern int dump_skip(struct coredump_params *cprm, size_t nr);
 extern int dump_emit(struct coredump_params *cprm, const void *addr, int nr);
 extern int dump_align(struct coredump_params *cprm, int align);
+extern void dump_truncate(struct coredump_params *cprm);
 #ifdef CONFIG_COREDUMP
 extern void do_coredump(const siginfo_t *siginfo);
 #else
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH RESEND] coredump: Ensure proper size of sparse core files
  2017-01-11 19:25 ` [PATCH RESEND] " Dave Kleikamp
@ 2017-01-17  3:42   ` David Miller
  2017-01-17  7:55     ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: David Miller @ 2017-01-17  3:42 UTC (permalink / raw)
  To: dave.kleikamp; +Cc: viro, sparclinux, linux-fsdevel, linux-kernel

From: Dave Kleikamp <dave.kleikamp@oracle.com>
Date: Wed, 11 Jan 2017 13:25:00 -0600

> If the last section of a core file ends with an unmapped or zero page,
> the size of the file does not correspond with the last dump_skip() call.
> gdb complains that the file is truncated and can be confusing to users.
> 
> After all of the vma sections are written, make sure that the file size
> is no smaller than the current file position.
> 
> This problem can be demonstrated with gdb's bigcore testcase on the
> sparc architecture.
> 
> Signed-off-by: Dave Kleikamp <dave.kleikamp@oracle.com>

The lseek() done by dump_skip() should extend the file properly.

Otherwise, everyone would be seeing this problem.

That test case passed all the time when I was last running the GDB
testsuite all the time on sparc64, say 5 years ago.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RESEND] coredump: Ensure proper size of sparse core files
  2017-01-17  3:42   ` David Miller
@ 2017-01-17  7:55     ` Christoph Hellwig
  2017-01-17 20:44       ` David Miller
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2017-01-17  7:55 UTC (permalink / raw)
  To: David Miller; +Cc: dave.kleikamp, viro, sparclinux, linux-fsdevel, linux-kernel

On Mon, Jan 16, 2017 at 10:42:43PM -0500, David Miller wrote:
> The lseek() done by dump_skip() should extend the file properly.

lseek never extends a file.  It just moves the current file position.
So if you do not have a write after the lseek it does nothing.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RESEND] coredump: Ensure proper size of sparse core files
  2017-01-17  7:55     ` Christoph Hellwig
@ 2017-01-17 20:44       ` David Miller
  2017-01-17 21:01         ` Dave Kleikamp
  0 siblings, 1 reply; 6+ messages in thread
From: David Miller @ 2017-01-17 20:44 UTC (permalink / raw)
  To: hch; +Cc: dave.kleikamp, viro, sparclinux, linux-fsdevel, linux-kernel

From: Christoph Hellwig <hch@infradead.org>
Date: Mon, 16 Jan 2017 23:55:21 -0800

> On Mon, Jan 16, 2017 at 10:42:43PM -0500, David Miller wrote:
>> The lseek() done by dump_skip() should extend the file properly.
> 
> lseek never extends a file.  It just moves the current file position.
> So if you do not have a write after the lseek it does nothing.

Ok, then the only other feedback I have is that we should probably
defer this truncate() call until the last possible dump write occurs
rather than right after the VMA loop.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RESEND] coredump: Ensure proper size of sparse core files
  2017-01-17 20:44       ` David Miller
@ 2017-01-17 21:01         ` Dave Kleikamp
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Kleikamp @ 2017-01-17 21:01 UTC (permalink / raw)
  To: David Miller, hch; +Cc: viro, sparclinux, linux-fsdevel, linux-kernel

On 01/17/2017 02:44 PM, David Miller wrote:
> From: Christoph Hellwig <hch@infradead.org>
> Date: Mon, 16 Jan 2017 23:55:21 -0800
> 
>> On Mon, Jan 16, 2017 at 10:42:43PM -0500, David Miller wrote:
>>> The lseek() done by dump_skip() should extend the file properly.
>>
>> lseek never extends a file.  It just moves the current file position.
>> So if you do not have a write after the lseek it does nothing.
> 
> Ok, then the only other feedback I have is that we should probably
> defer this truncate() call until the last possible dump write occurs
> rather than right after the VMA loop.

Yeah. That's fine with me.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-01-17 21:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-08 18:11 [PATCH] coredump: Ensure proper size of sparse core files Dave Kleikamp
2017-01-11 19:25 ` [PATCH RESEND] " Dave Kleikamp
2017-01-17  3:42   ` David Miller
2017-01-17  7:55     ` Christoph Hellwig
2017-01-17 20:44       ` David Miller
2017-01-17 21:01         ` Dave Kleikamp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).