2022-05-23 07:00:21

by Miguel Ojeda

[permalink] [raw]
Subject: [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size

From: Boqun Feng <[email protected]>

This makes it easier to update the size later on.

Furthermore, a static assert is added to ensure both are updated
when that happens. The relationship used is one that keeps the new
size (512+1) close to the original buffer size (500).

Reviewed-by: Kees Cook <[email protected]>
Signed-off-by: Boqun Feng <[email protected]>
Co-developed-by: Miguel Ojeda <[email protected]>
Signed-off-by: Miguel Ojeda <[email protected]>
---
This is a prerequisite patch, independently submitted at:

https://lore.kernel.org/lkml/[email protected]/

scripts/kallsyms.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 8caabddf817c..82d6508bdf29 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -27,8 +27,18 @@

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))

+#define _stringify_1(x) #x
+#define _stringify(x) _stringify_1(x)
+
#define KSYM_NAME_LEN 128

+/* A substantially bigger size than the current maximum. */
+#define KSYM_NAME_LEN_BUFFER 512
+_Static_assert(
+ KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
+ "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
+);
+
struct sym_entry {
unsigned long long addr;
unsigned int len;
@@ -197,15 +207,15 @@ static void check_symbol_range(const char *sym, unsigned long long addr,

static struct sym_entry *read_symbol(FILE *in)
{
- char name[500], type;
+ char name[KSYM_NAME_LEN_BUFFER+1], type;
unsigned long long addr;
unsigned int len;
struct sym_entry *sym;
int rc;

- rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
+ rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
if (rc != 3) {
- if (rc != EOF && fgets(name, 500, in) == NULL)
+ if (rc != EOF && fgets(name, sizeof(name), in) == NULL)
fprintf(stderr, "Read error or end of file.\n");
return NULL;
}
--
2.36.1



2022-05-23 21:17:27

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size

On Mon, May 23, 2022 at 04:01:14AM +0200, Miguel Ojeda wrote:
> From: Boqun Feng <[email protected]>
>
> This makes it easier to update the size later on.
>

This does not really conform to [1].

E.g.

"Declare KSY_NAME_LEN, which describes the maximum length for a kernel
symbol read by kallsyms from the input. In read_symbol(), define the
buffer to be of length "KSY_NAME_LEN + 1", which includes the terminator
character."

would be better.

> Furthermore, a static assert is added to ensure both are updated
> when that happens. The relationship used is one that keeps the new
> size (512+1) close to the original buffer size (500).

You must split this then into two patches:

1. A patch that re-defines the length with KSYM_NAME_LEN, i.e.
#define KSYM_NAME_LEN 499
2. A patch which increases the size and reasoning for that.

Right now bundles two separate changes, which does not conform to [2].

BR, Jarkko

2022-05-23 21:35:22

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size

On Mon, May 23, 2022 at 10:45:11PM +0300, Jarkko Sakkinen wrote:
> On Mon, May 23, 2022 at 04:01:14AM +0200, Miguel Ojeda wrote:
> > From: Boqun Feng <[email protected]>
> >
> > This makes it easier to update the size later on.
> >
>
> This does not really conform to [1].
>
> E.g.
>
> "Declare KSY_NAME_LEN, which describes the maximum length for a kernel
> symbol read by kallsyms from the input. In read_symbol(), define the
> buffer to be of length "KSY_NAME_LEN + 1", which includes the terminator
> character."
>
> would be better.
>
> > Furthermore, a static assert is added to ensure both are updated
> > when that happens. The relationship used is one that keeps the new
> > size (512+1) close to the original buffer size (500).
>
> You must split this then into two patches:
>
> 1. A patch that re-defines the length with KSYM_NAME_LEN, i.e.
> #define KSYM_NAME_LEN 499
> 2. A patch which increases the size and reasoning for that.
>
> Right now bundles two separate changes, which does not conform to [2].
>
> BR, Jarkko

The URL's:

[1] https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-changes
[2] https://www.kernel.org/doc/html/latest/process/submitting-patches.html#separate-your-changes

BR, Jarkko

2022-05-25 12:55:58

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size

On Mon, May 23, 2022 at 9:46 PM Jarkko Sakkinen <[email protected]> wrote:
>
> "Declare KSY_NAME_LEN, which describes the maximum length for a kernel
> symbol read by kallsyms from the input. In read_symbol(), define the
> buffer to be of length "KSY_NAME_LEN + 1", which includes the terminator
> character."
>
> would be better.

Note that the patch is not declaring `KSYM_NAME_LEN`, but a new
constant for a fairly arbitrarily sized for an input buffer.

I am all for detailed commit messages, and I agree this can be
expanded. However, I think the first sentence of what you wrote should
be part of the docs of the constant, and the second one sounds like it
could be a comment on the code. Something like "Introduce
KSYM_NAME_LEN_BUFFER in place of the previously hardcoded size of the
input buffer (...)" would be better for a reviewer.

> You must split this then into two patches:

Note that the size is not really being increased in a meaningful way
-- the important bit is the introduction of the relationship between
constants. The changes are all meant as a replacement for the
previously hardcoded constant, so I don't think the split is a "must",
but we can do it.

We can even split this into 3 patches: clean up the unneeded `sizeof`,
replace (and, importantly, document) the hardcoded constant, and
finally introduce the relationship.

Thanks for taking a look!

Cheers,
Miguel

2022-05-26 20:37:11

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH v7 01/25] kallsyms: avoid hardcoding the buffer size

On Tue, May 24, 2022 at 06:21:14PM +0200, Miguel Ojeda wrote:
> On Mon, May 23, 2022 at 9:46 PM Jarkko Sakkinen <[email protected]> wrote:
> >
> > "Declare KSY_NAME_LEN, which describes the maximum length for a kernel
> > symbol read by kallsyms from the input. In read_symbol(), define the
> > buffer to be of length "KSY_NAME_LEN + 1", which includes the terminator
> > character."
> >
> > would be better.
>
> Note that the patch is not declaring `KSYM_NAME_LEN`, but a new
> constant for a fairly arbitrarily sized for an input buffer.
>
> I am all for detailed commit messages, and I agree this can be
> expanded. However, I think the first sentence of what you wrote should
> be part of the docs of the constant, and the second one sounds like it
> could be a comment on the code. Something like "Introduce
> KSYM_NAME_LEN_BUFFER in place of the previously hardcoded size of the
> input buffer (...)" would be better for a reviewer.

Inline comment would be sufficient a remainder, and actually a better
idea.

> > You must split this then into two patches:
>
> Note that the size is not really being increased in a meaningful way
> -- the important bit is the introduction of the relationship between
> constants. The changes are all meant as a replacement for the
> previously hardcoded constant, so I don't think the split is a "must",
> but we can do it.
>
> We can even split this into 3 patches: clean up the unneeded `sizeof`,
> replace (and, importantly, document) the hardcoded constant, and
> finally introduce the relationship.
>
> Thanks for taking a look!
>
> Cheers,
> Miguel

BR, Jarkko