don't abort writing of a core dump after EFAULT

It's possible to get EFAULT when writing a segment backed by a file
if the segment extends beyond the file.
The core dump could still be useful if we skip the rest of the segment
and proceed to other segements.
The skipped segment (or a portion of it) will be zero-filled.

While there, use 'const' to signify that core_write() only reads the
buffer and use __DECONST before calling vn_rdwr_inchunks() because it
can be used for both reading and writing.

Before the change:
kernel: Failed to write core file for process mmap_trunc_core (error 14)
kernel: pid 77718 (mmap_trunc_core), uid 1001: exited on signal 6

After the change:
kernel: Failed to fully fault in a core file segment at VA 0x800645000 with size 0x4000 to be written at offset 0x29000 for process mmap_trunc_core
kernel: pid 4901 (mmap_trunc_core), uid 1001: exited on signal 6 (core dumped)

Reviewed by:	julian, kib
Obtained from:	Panzura (older version of the change)
MFC after:	5 days
Sponsored by:	Panzura
Differential Revision: https://reviews.freebsd.org/D9233
This commit is contained in:
avg 2017-01-20 13:39:07 +00:00
parent fa73e5b5c7
commit 05e4e60349

View File

@ -1160,7 +1160,7 @@ struct coredump_params {
static void cb_put_phdr(vm_map_entry_t, void *);
static void cb_size_segment(vm_map_entry_t, void *);
static int core_write(struct coredump_params *, void *, size_t, off_t,
static int core_write(struct coredump_params *, const void *, size_t, off_t,
enum uio_seg);
static void each_dumpable_segment(struct thread *, segment_callback, void *);
static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
@ -1202,7 +1202,14 @@ compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
while (len > 0) {
chunk_len = MIN(len, CORE_BUF_SIZE);
copyin(base, buf, chunk_len);
/*
* We can get EFAULT error here.
* In that case zero out the current chunk of the segment.
*/
error = copyin(base, buf, chunk_len);
if (error != 0)
bzero(buf, chunk_len);
error = gzio_write(p->gzs, buf, chunk_len);
if (error != 0)
break;
@ -1222,12 +1229,12 @@ core_gz_write(void *base, size_t len, off_t offset, void *arg)
#endif /* GZIO */
static int
core_write(struct coredump_params *p, void *base, size_t len, off_t offset,
enum uio_seg seg)
core_write(struct coredump_params *p, const void *base, size_t len,
off_t offset, enum uio_seg seg)
{
return (vn_rdwr_inchunks(UIO_WRITE, p->vp, base, len, offset,
seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base),
len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
p->active_cred, p->file_cred, NULL, p->td));
}
@ -1235,12 +1242,32 @@ static int
core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
void *tmpbuf)
{
int error;
#ifdef GZIO
if (p->gzs != NULL)
return (compress_chunk(p, base, tmpbuf, len));
#endif
return (core_write(p, base, len, offset, UIO_USERSPACE));
/*
* EFAULT is a non-fatal error that we can get, for example,
* if the segment is backed by a file but extends beyond its
* end.
*/
error = core_write(p, base, len, offset, UIO_USERSPACE);
if (error == EFAULT) {
log(LOG_WARNING, "Failed to fully fault in a core file segment "
"at VA %p with size 0x%zx to be written at offset 0x%jx "
"for process %s\n", base, len, offset, curproc->p_comm);
/*
* Write a "real" zero byte at the end of the target region
* in the case this is the last segment.
* The intermediate space will be implicitly zero-filled.
*/
error = core_write(p, zero_region, 1, offset + len - 1,
UIO_SYSSPACE);
}
return (error);
}
/*