2021-06-24 11:59:33

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

There are a number of printf "mismatches" in the use of die() in
x86/tools/relocs.c. Fix them up and add the printf attribute to the
reloc.h header file to prevent them from ever coming back.

Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: [email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
arch/x86/tools/relocs.c | 21 +++++++++++----------
arch/x86/tools/relocs.h | 1 +
2 files changed, 12 insertions(+), 10 deletions(-)

Originally sent back in Feb, but it seems to have been missed:
https://lore.kernel.org/r/[email protected]


diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index ce7188cbdae5..c3105a8c6cde 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -389,7 +389,8 @@ static void read_ehdr(FILE *fp)
Elf_Shdr shdr;

if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
+ die("Seek to %d failed: %s\n",
+ (int)ehdr.e_shoff, strerror(errno));

if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
die("Cannot read initial ELF section header: %s\n", strerror(errno));
@@ -412,17 +413,17 @@ static void read_shdrs(FILE *fp)

secs = calloc(shnum, sizeof(struct section));
if (!secs) {
- die("Unable to allocate %d section headers\n",
+ die("Unable to allocate %ld section headers\n",
shnum);
}
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
die("Seek to %d failed: %s\n",
- ehdr.e_shoff, strerror(errno));
+ (int)ehdr.e_shoff, strerror(errno));
}
for (i = 0; i < shnum; i++) {
struct section *sec = &secs[i];
if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
- die("Cannot read ELF section headers %d/%d: %s\n",
+ die("Cannot read ELF section headers %d/%ld: %s\n",
i, shnum, strerror(errno));
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
@@ -451,11 +452,11 @@ static void read_strtabs(FILE *fp)
sec->strtab = malloc(sec->shdr.sh_size);
if (!sec->strtab) {
die("malloc of %d bytes for strtab failed\n",
- sec->shdr.sh_size);
+ (int)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ (int)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -476,11 +477,11 @@ static void read_symtabs(FILE *fp)
sec->symtab = malloc(sec->shdr.sh_size);
if (!sec->symtab) {
die("malloc of %d bytes for symtab failed\n",
- sec->shdr.sh_size);
+ (int)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ (int)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -509,11 +510,11 @@ static void read_relocs(FILE *fp)
sec->reltab = malloc(sec->shdr.sh_size);
if (!sec->reltab) {
die("malloc of %d bytes for relocs failed\n",
- sec->shdr.sh_size);
+ (int)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ (int)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 43c83c0fd22c..4c49c82446eb 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -17,6 +17,7 @@
#include <regex.h>
#include <tools/le_byteshift.h>

+__attribute__((__format__(printf, 1, 2)))
void die(char *fmt, ...) __attribute__((noreturn));

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
--
2.30.1


2021-06-24 12:34:57

by Borislav Petkov

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Thu, Jun 24, 2021 at 01:58:03PM +0200, Greg Kroah-Hartman wrote:
> There are a number of printf "mismatches" in the use of die() in
> x86/tools/relocs.c. Fix them up and add the printf attribute to the
> reloc.h header file to prevent them from ever coming back.
>
> Cc: Thomas Gleixner <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: Borislav Petkov <[email protected]>
> Cc: "H. Peter Anvin" <[email protected]>
> Cc: [email protected]
> Signed-off-by: Greg Kroah-Hartman <[email protected]>
> ---
> arch/x86/tools/relocs.c | 21 +++++++++++----------
> arch/x86/tools/relocs.h | 1 +
> 2 files changed, 12 insertions(+), 10 deletions(-)
>
> Originally sent back in Feb, but it seems to have been missed:
> https://lore.kernel.org/r/[email protected]
>
>
> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> index ce7188cbdae5..c3105a8c6cde 100644
> --- a/arch/x86/tools/relocs.c
> +++ b/arch/x86/tools/relocs.c
> @@ -389,7 +389,8 @@ static void read_ehdr(FILE *fp)
> Elf_Shdr shdr;
>
> if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
> - die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
> + die("Seek to %d failed: %s\n",
> + (int)ehdr.e_shoff, strerror(errno));

Instead of casting all those, I think you should use %zu as, apparently,
we're using unsigned types for Elf{32,64}_Off and Elf{32,64}_Xword, etc.

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-06-24 14:45:37

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Thu, Jun 24, 2021 at 02:32:43PM +0200, Borislav Petkov wrote:
> On Thu, Jun 24, 2021 at 01:58:03PM +0200, Greg Kroah-Hartman wrote:
> > There are a number of printf "mismatches" in the use of die() in
> > x86/tools/relocs.c. Fix them up and add the printf attribute to the
> > reloc.h header file to prevent them from ever coming back.
> >
> > Cc: Thomas Gleixner <[email protected]>
> > Cc: Ingo Molnar <[email protected]>
> > Cc: Borislav Petkov <[email protected]>
> > Cc: "H. Peter Anvin" <[email protected]>
> > Cc: [email protected]
> > Signed-off-by: Greg Kroah-Hartman <[email protected]>
> > ---
> > arch/x86/tools/relocs.c | 21 +++++++++++----------
> > arch/x86/tools/relocs.h | 1 +
> > 2 files changed, 12 insertions(+), 10 deletions(-)
> >
> > Originally sent back in Feb, but it seems to have been missed:
> > https://lore.kernel.org/r/[email protected]
> >
> >
> > diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> > index ce7188cbdae5..c3105a8c6cde 100644
> > --- a/arch/x86/tools/relocs.c
> > +++ b/arch/x86/tools/relocs.c
> > @@ -389,7 +389,8 @@ static void read_ehdr(FILE *fp)
> > Elf_Shdr shdr;
> >
> > if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
> > - die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
> > + die("Seek to %d failed: %s\n",
> > + (int)ehdr.e_shoff, strerror(errno));
>
> Instead of casting all those, I think you should use %zu as, apparently,
> we're using unsigned types for Elf{32,64}_Off and Elf{32,64}_Xword, etc.

Odd, I thought I tried that and something did not work. Let me try it
again...

2021-06-25 12:08:25

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Thu, Jun 24, 2021 at 02:32:43PM +0200, Borislav Petkov wrote:
> On Thu, Jun 24, 2021 at 01:58:03PM +0200, Greg Kroah-Hartman wrote:
> > There are a number of printf "mismatches" in the use of die() in
> > x86/tools/relocs.c. Fix them up and add the printf attribute to the
> > reloc.h header file to prevent them from ever coming back.
> >
> > Cc: Thomas Gleixner <[email protected]>
> > Cc: Ingo Molnar <[email protected]>
> > Cc: Borislav Petkov <[email protected]>
> > Cc: "H. Peter Anvin" <[email protected]>
> > Cc: [email protected]
> > Signed-off-by: Greg Kroah-Hartman <[email protected]>
> > ---
> > arch/x86/tools/relocs.c | 21 +++++++++++----------
> > arch/x86/tools/relocs.h | 1 +
> > 2 files changed, 12 insertions(+), 10 deletions(-)
> >
> > Originally sent back in Feb, but it seems to have been missed:
> > https://lore.kernel.org/r/[email protected]
> >
> >
> > diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> > index ce7188cbdae5..c3105a8c6cde 100644
> > --- a/arch/x86/tools/relocs.c
> > +++ b/arch/x86/tools/relocs.c
> > @@ -389,7 +389,8 @@ static void read_ehdr(FILE *fp)
> > Elf_Shdr shdr;
> >
> > if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
> > - die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
> > + die("Seek to %d failed: %s\n",
> > + (int)ehdr.e_shoff, strerror(errno));
>
> Instead of casting all those, I think you should use %zu as, apparently,
> we're using unsigned types for Elf{32,64}_Off and Elf{32,64}_Xword, etc.

Ah, that does not work, I tried it and I get:

arch/x86/tools/relocs.c: In function ‘read_ehdr’:
arch/x86/tools/relocs.c:392:40: warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 2 has type ‘Elf32_Off’ {aka ‘unsigned int’} [-Wformat=]
392 | die("Seek to %zu failed: %s\n",
| ~~^
| |
| long unsigned int
| %u
393 | ehdr.e_shoff, strerror(errno));
| ~~~~~~~~~~~~
| |
| Elf32_Off {aka unsigned int}

Casting seems to be the only way to make this "quiet" that I can tell.

Unless someone else has a good idea?

thanks,

greg k-h

2021-06-25 14:15:30

by Borislav Petkov

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Fri, Jun 25, 2021 at 02:06:59PM +0200, Greg Kroah-Hartman wrote:
> Casting seems to be the only way to make this "quiet" that I can tell.
>
> Unless someone else has a good idea?

Hmm, so in Documentation/core-api/printk-formats.rst we say that for
printk() with different size types, we should "use a format specifier of
its largest possible type and explicitly cast to it."

And that kinda sounds ok to me because we don't potentially lose through
the casting.

IOW, I guess something like this below.

---
diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 04c5a44b9682..42b0f425a2c7 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -389,7 +389,7 @@ static void read_ehdr(FILE *fp)
Elf_Shdr shdr;

if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
+ die("Seek to %lu failed: %s\n", (unsigned long)ehdr.e_shoff, strerror(errno));

if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
die("Cannot read initial ELF section header: %s\n", strerror(errno));
@@ -412,17 +412,17 @@ static void read_shdrs(FILE *fp)

secs = calloc(shnum, sizeof(struct section));
if (!secs) {
- die("Unable to allocate %d section headers\n",
+ die("Unable to allocate %ld section headers\n",
shnum);
}
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- ehdr.e_shoff, strerror(errno));
+ die("Seek to %lu failed: %s\n",
+ (unsigned long)ehdr.e_shoff, strerror(errno));
}
for (i = 0; i < shnum; i++) {
struct section *sec = &secs[i];
if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
- die("Cannot read ELF section headers %d/%d: %s\n",
+ die("Cannot read ELF section headers %d/%ld: %s\n",
i, shnum, strerror(errno));
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
@@ -450,12 +450,12 @@ static void read_strtabs(FILE *fp)
}
sec->strtab = malloc(sec->shdr.sh_size);
if (!sec->strtab) {
- die("malloc of %d bytes for strtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %lu bytes for strtab failed\n",
+ (unsigned long)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %lu failed: %s\n",
+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -475,12 +475,12 @@ static void read_symtabs(FILE *fp)
}
sec->symtab = malloc(sec->shdr.sh_size);
if (!sec->symtab) {
- die("malloc of %d bytes for symtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %lu bytes for symtab failed\n",
+ (unsigned long)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %lu failed: %s\n",
+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -508,12 +508,12 @@ static void read_relocs(FILE *fp)
}
sec->reltab = malloc(sec->shdr.sh_size);
if (!sec->reltab) {
- die("malloc of %d bytes for relocs failed\n",
- sec->shdr.sh_size);
+ die("malloc of %lu bytes for relocs failed\n",
+ (unsigned long)sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %lu failed: %s\n",
+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 43c83c0fd22c..4c49c82446eb 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -17,6 +17,7 @@
#include <regex.h>
#include <tools/le_byteshift.h>

+__attribute__((__format__(printf, 1, 2)))
void die(char *fmt, ...) __attribute__((noreturn));

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

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-06-25 16:19:55

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

%zu wouldn't DTRT for cross build.

On June 24, 2021 7:44:02 AM PDT, Greg Kroah-Hartman <[email protected]> wrote:
>On Thu, Jun 24, 2021 at 02:32:43PM +0200, Borislav Petkov wrote:
>> On Thu, Jun 24, 2021 at 01:58:03PM +0200, Greg Kroah-Hartman wrote:
>> > There are a number of printf "mismatches" in the use of die() in
>> > x86/tools/relocs.c. Fix them up and add the printf attribute to
>the
>> > reloc.h header file to prevent them from ever coming back.
>> >
>> > Cc: Thomas Gleixner <[email protected]>
>> > Cc: Ingo Molnar <[email protected]>
>> > Cc: Borislav Petkov <[email protected]>
>> > Cc: "H. Peter Anvin" <[email protected]>
>> > Cc: [email protected]
>> > Signed-off-by: Greg Kroah-Hartman <[email protected]>
>> > ---
>> > arch/x86/tools/relocs.c | 21 +++++++++++----------
>> > arch/x86/tools/relocs.h | 1 +
>> > 2 files changed, 12 insertions(+), 10 deletions(-)
>> >
>> > Originally sent back in Feb, but it seems to have been missed:
>> >
> https://lore.kernel.org/r/[email protected]
>> >
>> >
>> > diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>> > index ce7188cbdae5..c3105a8c6cde 100644
>> > --- a/arch/x86/tools/relocs.c
>> > +++ b/arch/x86/tools/relocs.c
>> > @@ -389,7 +389,8 @@ static void read_ehdr(FILE *fp)
>> > Elf_Shdr shdr;
>> >
>> > if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
>> > - die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
>> > + die("Seek to %d failed: %s\n",
>> > + (int)ehdr.e_shoff, strerror(errno));
>>
>> Instead of casting all those, I think you should use %zu as,
>apparently,
>> we're using unsigned types for Elf{32,64}_Off and Elf{32,64}_Xword,
>etc.
>
>Odd, I thought I tried that and something did not work. Let me try it
>again...

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

2021-06-25 16:20:38

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

This is a user space build time tool.

You can use PRIu32/64 or cast to unsigned long long; it's not like the performance for this case is going to matter one iota.

On June 25, 2021 7:12:36 AM PDT, Borislav Petkov <[email protected]> wrote:
>On Fri, Jun 25, 2021 at 02:06:59PM +0200, Greg Kroah-Hartman wrote:
>> Casting seems to be the only way to make this "quiet" that I can
>tell.
>>
>> Unless someone else has a good idea?
>
>Hmm, so in Documentation/core-api/printk-formats.rst we say that for
>printk() with different size types, we should "use a format specifier
>of
>its largest possible type and explicitly cast to it."
>
>And that kinda sounds ok to me because we don't potentially lose
>through
>the casting.
>
>IOW, I guess something like this below.
>
>---
>diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>index 04c5a44b9682..42b0f425a2c7 100644
>--- a/arch/x86/tools/relocs.c
>+++ b/arch/x86/tools/relocs.c
>@@ -389,7 +389,7 @@ static void read_ehdr(FILE *fp)
> Elf_Shdr shdr;
>
> if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
>- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
>+ die("Seek to %lu failed: %s\n", (unsigned long)ehdr.e_shoff,
>strerror(errno));
>
> if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
> die("Cannot read initial ELF section header: %s\n",
>strerror(errno));
>@@ -412,17 +412,17 @@ static void read_shdrs(FILE *fp)
>
> secs = calloc(shnum, sizeof(struct section));
> if (!secs) {
>- die("Unable to allocate %d section headers\n",
>+ die("Unable to allocate %ld section headers\n",
> shnum);
> }
> if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
>- die("Seek to %d failed: %s\n",
>- ehdr.e_shoff, strerror(errno));
>+ die("Seek to %lu failed: %s\n",
>+ (unsigned long)ehdr.e_shoff, strerror(errno));
> }
> for (i = 0; i < shnum; i++) {
> struct section *sec = &secs[i];
> if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
>- die("Cannot read ELF section headers %d/%d: %s\n",
>+ die("Cannot read ELF section headers %d/%ld: %s\n",
> i, shnum, strerror(errno));
> sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
> sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
>@@ -450,12 +450,12 @@ static void read_strtabs(FILE *fp)
> }
> sec->strtab = malloc(sec->shdr.sh_size);
> if (!sec->strtab) {
>- die("malloc of %d bytes for strtab failed\n",
>- sec->shdr.sh_size);
>+ die("malloc of %lu bytes for strtab failed\n",
>+ (unsigned long)sec->shdr.sh_size);
> }
> if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>- die("Seek to %d failed: %s\n",
>- sec->shdr.sh_offset, strerror(errno));
>+ die("Seek to %lu failed: %s\n",
>+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
> }
> if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
> != sec->shdr.sh_size) {
>@@ -475,12 +475,12 @@ static void read_symtabs(FILE *fp)
> }
> sec->symtab = malloc(sec->shdr.sh_size);
> if (!sec->symtab) {
>- die("malloc of %d bytes for symtab failed\n",
>- sec->shdr.sh_size);
>+ die("malloc of %lu bytes for symtab failed\n",
>+ (unsigned long)sec->shdr.sh_size);
> }
> if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>- die("Seek to %d failed: %s\n",
>- sec->shdr.sh_offset, strerror(errno));
>+ die("Seek to %lu failed: %s\n",
>+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
> }
> if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
> != sec->shdr.sh_size) {
>@@ -508,12 +508,12 @@ static void read_relocs(FILE *fp)
> }
> sec->reltab = malloc(sec->shdr.sh_size);
> if (!sec->reltab) {
>- die("malloc of %d bytes for relocs failed\n",
>- sec->shdr.sh_size);
>+ die("malloc of %lu bytes for relocs failed\n",
>+ (unsigned long)sec->shdr.sh_size);
> }
> if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
>- die("Seek to %d failed: %s\n",
>- sec->shdr.sh_offset, strerror(errno));
>+ die("Seek to %lu failed: %s\n",
>+ (unsigned long)sec->shdr.sh_offset, strerror(errno));
> }
> if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
> != sec->shdr.sh_size) {
>diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
>index 43c83c0fd22c..4c49c82446eb 100644
>--- a/arch/x86/tools/relocs.h
>+++ b/arch/x86/tools/relocs.h
>@@ -17,6 +17,7 @@
> #include <regex.h>
> #include <tools/le_byteshift.h>
>
>+__attribute__((__format__(printf, 1, 2)))
> void die(char *fmt, ...) __attribute__((noreturn));
>
> #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

2021-06-25 16:55:57

by Borislav Petkov

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Fri, Jun 25, 2021 at 09:19:38AM -0700, H. Peter Anvin wrote:
> You can use PRIu32/64 or cast to unsigned long long; it's not like the
> performance for this case is going to matter one iota.

Why "unsigned long long"?

Those fields are typedeffed as:

typedef __u32 Elf32_Off;

or

typedef __u64 Elf64_Off;

respectively so they should fit in an "unsigned long" on the respective
width.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-06-25 20:40:46

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

64-bit cross build on a 32-bit platform... or Windows.

On June 25, 2021 9:53:10 AM PDT, Borislav Petkov <[email protected]> wrote:
>On Fri, Jun 25, 2021 at 09:19:38AM -0700, H. Peter Anvin wrote:
>> You can use PRIu32/64 or cast to unsigned long long; it's not like
>the
>> performance for this case is going to matter one iota.
>
>Why "unsigned long long"?
>
>Those fields are typedeffed as:
>
>typedef __u32 Elf32_Off;
>
>or
>
>typedef __u64 Elf64_Off;
>
>respectively so they should fit in an "unsigned long" on the respective
>width.

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

2021-06-25 21:09:25

by Borislav Petkov

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Fri, Jun 25, 2021 at 01:38:51PM -0700, H. Peter Anvin wrote:
> 64-bit cross build on a 32-bit platform... or Windows.

Meh, nobody cares about those... :)

Hmm, so looking at the PRI* inttypes.h things again, they're C99 and
they kinda look more elegant as they don't make us cast stuff.

So how does that look?

---

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 04c5a44b9682..2582991ba216 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -26,6 +26,9 @@ static struct relocs relocs32;
#if ELF_BITS == 64
static struct relocs relocs32neg;
static struct relocs relocs64;
+#define FMT PRIu64
+#else
+#define FMT PRIu32
#endif

struct section {
@@ -389,7 +392,7 @@ static void read_ehdr(FILE *fp)
Elf_Shdr shdr;

if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));

if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
die("Cannot read initial ELF section header: %s\n", strerror(errno));
@@ -412,17 +415,17 @@ static void read_shdrs(FILE *fp)

secs = calloc(shnum, sizeof(struct section));
if (!secs) {
- die("Unable to allocate %d section headers\n",
+ die("Unable to allocate %ld section headers\n",
shnum);
}
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ ehdr.e_shoff, strerror(errno));
}
for (i = 0; i < shnum; i++) {
struct section *sec = &secs[i];
if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
- die("Cannot read ELF section headers %d/%d: %s\n",
+ die("Cannot read ELF section headers %d/%ld: %s\n",
i, shnum, strerror(errno));
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
@@ -450,12 +453,12 @@ static void read_strtabs(FILE *fp)
}
sec->strtab = malloc(sec->shdr.sh_size);
if (!sec->strtab) {
- die("malloc of %d bytes for strtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for strtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -475,12 +478,12 @@ static void read_symtabs(FILE *fp)
}
sec->symtab = malloc(sec->shdr.sh_size);
if (!sec->symtab) {
- die("malloc of %d bytes for symtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for symtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -508,12 +511,12 @@ static void read_relocs(FILE *fp)
}
sec->reltab = malloc(sec->shdr.sh_size);
if (!sec->reltab) {
- die("malloc of %d bytes for relocs failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for relocs failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 43c83c0fd22c..4c49c82446eb 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -17,6 +17,7 @@
#include <regex.h>
#include <tools/le_byteshift.h>

+__attribute__((__format__(printf, 1, 2)))
void die(char *fmt, ...) __attribute__((noreturn));

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

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-06-27 15:04:35

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RESEND PATCH] x86/tools/relocs: add __printf attribute to die()

On Fri, Jun 25, 2021 at 11:07:28PM +0200, Borislav Petkov wrote:
> On Fri, Jun 25, 2021 at 01:38:51PM -0700, H. Peter Anvin wrote:
> > 64-bit cross build on a 32-bit platform... or Windows.
>
> Meh, nobody cares about those... :)
>
> Hmm, so looking at the PRI* inttypes.h things again, they're C99 and
> they kinda look more elegant as they don't make us cast stuff.
>
> So how does that look?
>
> ---
>
> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> index 04c5a44b9682..2582991ba216 100644
> --- a/arch/x86/tools/relocs.c
> +++ b/arch/x86/tools/relocs.c
> @@ -26,6 +26,9 @@ static struct relocs relocs32;
> #if ELF_BITS == 64
> static struct relocs relocs32neg;
> static struct relocs relocs64;
> +#define FMT PRIu64
> +#else
> +#define FMT PRIu32
> #endif

<snip>

This works for me! It should fix the static checking tool that keeps
tripping over this pointless warning :)

Want to turn it into a real patch?

thanks,

greg k-h

2021-06-28 18:56:54

by Borislav Petkov

[permalink] [raw]
Subject: [PATCH] x86/tools/relocs: Mark die() with the printf function attr format

On Sun, Jun 27, 2021 at 05:01:28PM +0200, Greg Kroah-Hartman wrote:
> This works for me! It should fix the static checking tool that keeps
> tripping over this pointless warning :)
>
> Want to turn it into a real patch?

How's that?

---
From: Borislav Petkov <[email protected]>

Mark die() as a function which accepts printf-style arguments so that
the compiler can typecheck them against the supplied format string.

Use the C99 inttypes.h format specifiers as relocs.c gets built for both
32- and 64-bit.

Original version of the patch by Greg Kroah-Hartman <[email protected]>

Signed-off-by: Borislav Petkov <[email protected]>
---
arch/x86/tools/relocs.c | 37 ++++++++++++++++++++-----------------
arch/x86/tools/relocs.h | 1 +
2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 04c5a44b9682..2582991ba216 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -26,6 +26,9 @@ static struct relocs relocs32;
#if ELF_BITS == 64
static struct relocs relocs32neg;
static struct relocs relocs64;
+#define FMT PRIu64
+#else
+#define FMT PRIu32
#endif

struct section {
@@ -389,7 +392,7 @@ static void read_ehdr(FILE *fp)
Elf_Shdr shdr;

if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));

if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
die("Cannot read initial ELF section header: %s\n", strerror(errno));
@@ -412,17 +415,17 @@ static void read_shdrs(FILE *fp)

secs = calloc(shnum, sizeof(struct section));
if (!secs) {
- die("Unable to allocate %d section headers\n",
+ die("Unable to allocate %ld section headers\n",
shnum);
}
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ ehdr.e_shoff, strerror(errno));
}
for (i = 0; i < shnum; i++) {
struct section *sec = &secs[i];
if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
- die("Cannot read ELF section headers %d/%d: %s\n",
+ die("Cannot read ELF section headers %d/%ld: %s\n",
i, shnum, strerror(errno));
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
@@ -450,12 +453,12 @@ static void read_strtabs(FILE *fp)
}
sec->strtab = malloc(sec->shdr.sh_size);
if (!sec->strtab) {
- die("malloc of %d bytes for strtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for strtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -475,12 +478,12 @@ static void read_symtabs(FILE *fp)
}
sec->symtab = malloc(sec->shdr.sh_size);
if (!sec->symtab) {
- die("malloc of %d bytes for symtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for symtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -508,12 +511,12 @@ static void read_relocs(FILE *fp)
}
sec->reltab = malloc(sec->shdr.sh_size);
if (!sec->reltab) {
- die("malloc of %d bytes for relocs failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for relocs failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 43c83c0fd22c..4c49c82446eb 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -17,6 +17,7 @@
#include <regex.h>
#include <tools/le_byteshift.h>

+__attribute__((__format__(printf, 1, 2)))
void die(char *fmt, ...) __attribute__((noreturn));

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
--
2.29.2

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Subject: [tip: x86/build] x86/tools/relocs: Mark die() with the printf function attr format

The following commit has been merged into the x86/build branch of tip:

Commit-ID: 03dca99e200f4d268f70079cf54e3b1200c9eb9d
Gitweb: https://git.kernel.org/tip/03dca99e200f4d268f70079cf54e3b1200c9eb9d
Author: Borislav Petkov <[email protected]>
AuthorDate: Fri, 25 Jun 2021 17:10:16 +02:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Mon, 23 Aug 2021 05:58:02 +02:00

x86/tools/relocs: Mark die() with the printf function attr format

Mark die() as a function which accepts printf-style arguments so that
the compiler can typecheck them against the supplied format string.

Use the C99 inttypes.h format specifiers as relocs.c gets built for both
32- and 64-bit.

Original version of the patch by Greg Kroah-Hartman <[email protected]>

Signed-off-by: Borislav Petkov <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
---
arch/x86/tools/relocs.c | 37 ++++++++++++++++++++-----------------
arch/x86/tools/relocs.h | 1 +
2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 9ba700d..27c8220 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -26,6 +26,9 @@ static struct relocs relocs32;
#if ELF_BITS == 64
static struct relocs relocs32neg;
static struct relocs relocs64;
+#define FMT PRIu64
+#else
+#define FMT PRIu32
#endif

struct section {
@@ -389,7 +392,7 @@ static void read_ehdr(FILE *fp)
Elf_Shdr shdr;

if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
- die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));

if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
die("Cannot read initial ELF section header: %s\n", strerror(errno));
@@ -412,17 +415,17 @@ static void read_shdrs(FILE *fp)

secs = calloc(shnum, sizeof(struct section));
if (!secs) {
- die("Unable to allocate %d section headers\n",
+ die("Unable to allocate %ld section headers\n",
shnum);
}
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- ehdr.e_shoff, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ ehdr.e_shoff, strerror(errno));
}
for (i = 0; i < shnum; i++) {
struct section *sec = &secs[i];
if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
- die("Cannot read ELF section headers %d/%d: %s\n",
+ die("Cannot read ELF section headers %d/%ld: %s\n",
i, shnum, strerror(errno));
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
@@ -450,12 +453,12 @@ static void read_strtabs(FILE *fp)
}
sec->strtab = malloc(sec->shdr.sh_size);
if (!sec->strtab) {
- die("malloc of %d bytes for strtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for strtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -475,12 +478,12 @@ static void read_symtabs(FILE *fp)
}
sec->symtab = malloc(sec->shdr.sh_size);
if (!sec->symtab) {
- die("malloc of %d bytes for symtab failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for symtab failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
@@ -508,12 +511,12 @@ static void read_relocs(FILE *fp)
}
sec->reltab = malloc(sec->shdr.sh_size);
if (!sec->reltab) {
- die("malloc of %d bytes for relocs failed\n",
- sec->shdr.sh_size);
+ die("malloc of %" FMT " bytes for relocs failed\n",
+ sec->shdr.sh_size);
}
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
- die("Seek to %d failed: %s\n",
- sec->shdr.sh_offset, strerror(errno));
+ die("Seek to %" FMT " failed: %s\n",
+ sec->shdr.sh_offset, strerror(errno));
}
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
!= sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 43c83c0..4c49c82 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -17,6 +17,7 @@
#include <regex.h>
#include <tools/le_byteshift.h>

+__attribute__((__format__(printf, 1, 2)))
void die(char *fmt, ...) __attribute__((noreturn));

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