linux/drivers/xen/xenfs/xensyms.c
Jan Beulich 5c4e79e29a xenfs/xensyms: respect hypervisor's "next" indication
The interface specifies the symnum field as an input and output; the
hypervisor sets it to the next sequential symbol's index. xensyms_next()
incrementing the position explicitly (and xensyms_next_sym()
decrementing it to "rewind") is only correct as long as the sequence of
symbol indexes is non-sparse. Use the hypervisor-supplied value instead
to update the position in xensyms_next(), and use the saved incoming
index in xensyms_next_sym().

Cc: stable@kernel.org
Fixes: a11f4f0a4e ("xen: xensyms support")
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Message-ID: <15d5e7fa-ec5d-422f-9319-d28bed916349@suse.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
2025-03-14 11:18:59 +01:00

152 lines
3 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <linux/init.h>
#include <linux/seq_file.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <xen/interface/platform.h>
#include <asm/xen/hypercall.h>
#include <xen/xen-ops.h>
#include "xenfs.h"
#define XEN_KSYM_NAME_LEN 127 /* Hypervisor may have different name length */
struct xensyms {
struct xen_platform_op op;
char *name;
uint32_t namelen;
};
/* Grab next output page from the hypervisor */
static int xensyms_next_sym(struct xensyms *xs)
{
int ret;
struct xenpf_symdata *symdata = &xs->op.u.symdata;
uint64_t symnum;
memset(xs->name, 0, xs->namelen);
symdata->namelen = xs->namelen;
symnum = symdata->symnum;
ret = HYPERVISOR_platform_op(&xs->op);
if (ret < 0)
return ret;
/*
* If hypervisor's symbol didn't fit into the buffer then allocate
* a larger buffer and try again.
*/
if (unlikely(symdata->namelen > xs->namelen)) {
kfree(xs->name);
xs->namelen = symdata->namelen;
xs->name = kzalloc(xs->namelen, GFP_KERNEL);
if (!xs->name)
return -ENOMEM;
set_xen_guest_handle(symdata->name, xs->name);
symdata->symnum = symnum; /* Rewind */
ret = HYPERVISOR_platform_op(&xs->op);
if (ret < 0)
return ret;
}
if (symdata->symnum == symnum)
/* End of symbols */
return 1;
return 0;
}
static void *xensyms_start(struct seq_file *m, loff_t *pos)
{
struct xensyms *xs = m->private;
xs->op.u.symdata.symnum = *pos;
if (xensyms_next_sym(xs))
return NULL;
return m->private;
}
static void *xensyms_next(struct seq_file *m, void *p, loff_t *pos)
{
struct xensyms *xs = m->private;
*pos = xs->op.u.symdata.symnum;
if (xensyms_next_sym(xs))
return NULL;
return p;
}
static int xensyms_show(struct seq_file *m, void *p)
{
struct xensyms *xs = m->private;
struct xenpf_symdata *symdata = &xs->op.u.symdata;
seq_printf(m, "%016llx %c %s\n", symdata->address,
symdata->type, xs->name);
return 0;
}
static void xensyms_stop(struct seq_file *m, void *p)
{
}
static const struct seq_operations xensyms_seq_ops = {
.start = xensyms_start,
.next = xensyms_next,
.show = xensyms_show,
.stop = xensyms_stop,
};
static int xensyms_open(struct inode *inode, struct file *file)
{
struct seq_file *m;
struct xensyms *xs;
int ret;
ret = seq_open_private(file, &xensyms_seq_ops,
sizeof(struct xensyms));
if (ret)
return ret;
m = file->private_data;
xs = m->private;
xs->namelen = XEN_KSYM_NAME_LEN + 1;
xs->name = kzalloc(xs->namelen, GFP_KERNEL);
if (!xs->name) {
seq_release_private(inode, file);
return -ENOMEM;
}
set_xen_guest_handle(xs->op.u.symdata.name, xs->name);
xs->op.cmd = XENPF_get_symbol;
xs->op.u.symdata.namelen = xs->namelen;
return 0;
}
static int xensyms_release(struct inode *inode, struct file *file)
{
struct seq_file *m = file->private_data;
struct xensyms *xs = m->private;
kfree(xs->name);
return seq_release_private(inode, file);
}
const struct file_operations xensyms_ops = {
.open = xensyms_open,
.read = seq_read,
.llseek = seq_lseek,
.release = xensyms_release
};