Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1285990 > unrolled thread

[PATCH 02/12] selftests/x86: Add check_initial_reg_state

Started byAndy Lutomirski <luto@kernel.org>
First post2015-12-07 23:00 +0100
Last post2015-12-09 20:30 +0100
Articles 6 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 02/12] selftests/x86: Add check_initial_reg_state Andy Lutomirski <luto@kernel.org> - 2015-12-07 23:00 +0100
    Re: [PATCH 02/12] selftests/x86: Add check_initial_reg_state Borislav Petkov <bp@alien8.de> - 2015-12-08 11:00 +0100
      Re: [PATCH 02/12] selftests/x86: Add check_initial_reg_state Andy Lutomirski <luto@amacapital.net> - 2015-12-09 20:00 +0100
        Re: [PATCH 02/12] selftests/x86: Add check_initial_reg_state Borislav Petkov <bp@alien8.de> - 2015-12-09 20:10 +0100
          Re: [PATCH 02/12] selftests/x86: Add check_initial_reg_state Borislav Petkov <bp@alien8.de> - 2015-12-09 20:30 +0100
          Re: [PATCH 02/12] selftests/x86: Add check_initial_reg_state Andy Lutomirski <luto@amacapital.net> - 2015-12-09 20:30 +0100

#1285990 — [PATCH 02/12] selftests/x86: Add check_initial_reg_state

FromAndy Lutomirski <luto@kernel.org>
Date2015-12-07 23:00 +0100
Subject[PATCH 02/12] selftests/x86: Add check_initial_reg_state
Message-ID<qDbZ9-1Mo-49@gated-at.bofh.it>
This checks that ELF binaries are started with an appropriately
blank register state.

(There's currently a nasty special case in the entry asm to arrange
 for this.  I'm planning on removing the special case, and this will
 help make sure I don't break it.)

Signed-off-by: Andy Lutomirski <luto@kernel.org>
---
 tools/testing/selftests/x86/Makefile               |   8 +-
 .../selftests/x86/check_initial_reg_state.c        | 108 +++++++++++++++++++++
 2 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/check_initial_reg_state.c

diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
index a460fe7c5365..b82409421fa6 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -4,7 +4,7 @@ include ../lib.mk
 
 .PHONY: all all_32 all_64 warn_32bit_failure clean
 
-TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall
+TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall check_initial_reg_state
 TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault sigreturn test_syscall_vdso unwind_vdso
 
 TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
@@ -63,3 +63,9 @@ endif
 sysret_ss_attrs_64: thunks.S
 ptrace_syscall_32: raw_syscall_helper_32.S
 test_syscall_vdso_32: thunks_32.S
+
+# check_initial_reg_state is special: it needs a custom entry, and it
+# needs to be static so that its interpreter doesn't destroy its initial
+# state.
+check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
+check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
diff --git a/tools/testing/selftests/x86/check_initial_reg_state.c b/tools/testing/selftests/x86/check_initial_reg_state.c
new file mode 100644
index 000000000000..0cb565f7786d
--- /dev/null
+++ b/tools/testing/selftests/x86/check_initial_reg_state.c
@@ -0,0 +1,108 @@
+/*
+ * check_initial_reg_state.c - check that execve sets the correct state
+ * Copyright (c) 2014-2015 Andrew Lutomirski
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+
+unsigned long ax, bx, cx, dx, si, di, bp, sp, flags;
+unsigned long r8, r9, r10, r11, r12, r13, r14, r15;
+
+asm (".pushsection .text\n\t"
+     ".type real_start, @function\n\t"
+     ".global real_start\n\t"
+     "real_start:\n\t"
+#ifdef __x86_64__
+     "mov %rax, ax\n\t"
+     "mov %rbx, bx\n\t"
+     "mov %rcx, cx\n\t"
+     "mov %rdx, dx\n\t"
+     "mov %rsi, si\n\t"
+     "mov %rdi, di\n\t"
+     "mov %rbp, bp\n\t"
+     "mov %rsp, sp\n\t"
+     "mov %r8, r8\n\t"
+     "mov %r9, r9\n\t"
+     "mov %r10, r10\n\t"
+     "mov %r11, r11\n\t"
+     "mov %r12, r12\n\t"
+     "mov %r13, r13\n\t"
+     "mov %r14, r14\n\t"
+     "mov %r15, r15\n\t"
+     "pushfq\n\t"
+     "popq flags\n\t"
+#else
+     "mov %eax, ax\n\t"
+     "mov %ebx, bx\n\t"
+     "mov %ecx, cx\n\t"
+     "mov %edx, dx\n\t"
+     "mov %esi, si\n\t"
+     "mov %edi, di\n\t"
+     "mov %ebp, bp\n\t"
+     "mov %esp, sp\n\t"
+     "pushfl\n\t"
+     "popl flags\n\t"
+#endif
+     "jmp _start\n\t"
+     ".size real_start, . - real_start\n\t"
+     ".popsection");
+
+int main()
+{
+	int nerrs = 0;
+
+	if (sp == 0) {
+		printf("[FAIL]\tTest was built incorrectly\n");
+		return 1;
+	}
+
+	if (ax || bx || cx || dx || si || di || bp
+#ifdef __x86_64__
+	    || r8 || r9 || r10 || r11 || r12 || r13 || r14 || r15
+#endif
+		) {
+		printf("[FAIL]\tAll GPRs except SP should be 0\n");
+#define SHOW(x) printf("\t" #x " = 0x%lx\n", x);
+		SHOW(ax);
+		SHOW(bx);
+		SHOW(cx);
+		SHOW(dx);
+		SHOW(si);
+		SHOW(di);
+		SHOW(bp);
+		SHOW(sp);
+#ifdef __x86_64__
+		SHOW(r8);
+		SHOW(r9);
+		SHOW(r10);
+		SHOW(r11);
+		SHOW(r12);
+		SHOW(r13);
+		SHOW(r14);
+		SHOW(r15);
+#endif
+		nerrs++;
+	} else {
+		printf("[OK]\tAll GPRs except SP are 0\n");
+	}
+
+	if (flags != 0x202) {
+		printf("[FAIL]\tFLAGS is 0x%lx, but it should be 0x202\n", flags);
+		nerrs++;
+	} else {
+		printf("[OK]\tFLAGS is 0x202\n");
+	}
+
+	return nerrs ? 1 : 0;
+}
-- 
2.5.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1286355

FromBorislav Petkov <bp@alien8.de>
Date2015-12-08 11:00 +0100
Message-ID<qDndU-AW-13@gated-at.bofh.it>
In reply to#1285990
On Mon, Dec 07, 2015 at 01:51:27PM -0800, Andy Lutomirski wrote:
> This checks that ELF binaries are started with an appropriately
> blank register state.
> 
> (There's currently a nasty special case in the entry asm to arrange
>  for this.  I'm planning on removing the special case, and this will
>  help make sure I don't break it.)
> 
> Signed-off-by: Andy Lutomirski <luto@kernel.org>
> ---
>  tools/testing/selftests/x86/Makefile               |   8 +-
>  .../selftests/x86/check_initial_reg_state.c        | 108 +++++++++++++++++++++
>  2 files changed, 115 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/x86/check_initial_reg_state.c
> 
> diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
> index a460fe7c5365..b82409421fa6 100644
> --- a/tools/testing/selftests/x86/Makefile
> +++ b/tools/testing/selftests/x86/Makefile
> @@ -4,7 +4,7 @@ include ../lib.mk
>  
>  .PHONY: all all_32 all_64 warn_32bit_failure clean
>  
> -TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall
> +TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall check_initial_reg_state
>  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault sigreturn test_syscall_vdso unwind_vdso
>  
>  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
> @@ -63,3 +63,9 @@ endif
>  sysret_ss_attrs_64: thunks.S
>  ptrace_syscall_32: raw_syscall_helper_32.S
>  test_syscall_vdso_32: thunks_32.S
> +
> +# check_initial_reg_state is special: it needs a custom entry, and it
> +# needs to be static so that its interpreter doesn't destroy its initial
> +# state.
> +check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
> +check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
> diff --git a/tools/testing/selftests/x86/check_initial_reg_state.c b/tools/testing/selftests/x86/check_initial_reg_state.c
> new file mode 100644
> index 000000000000..0cb565f7786d
> --- /dev/null
> +++ b/tools/testing/selftests/x86/check_initial_reg_state.c
> @@ -0,0 +1,108 @@
> +/*
> + * check_initial_reg_state.c - check that execve sets the correct state
> + * Copyright (c) 2014-2015 Andrew Lutomirski
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + */
> +
> +#define _GNU_SOURCE
> +
> +#include <stdio.h>
> +
> +unsigned long ax, bx, cx, dx, si, di, bp, sp, flags;
> +unsigned long r8, r9, r10, r11, r12, r13, r14, r15;
> +
> +asm (".pushsection .text\n\t"

WARNING: please, no spaces at the start of a line
#82: FILE: tools/testing/selftests/x86/check_initial_reg_state.c:23:
+     ".type real_start, @function\n\t"$

Something trampled over the preceding tabs in that whole asm().

-- 
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1287741

FromAndy Lutomirski <luto@amacapital.net>
Date2015-12-09 20:00 +0100
Message-ID<qDS82-3Fq-21@gated-at.bofh.it>
In reply to#1286355
On Tue, Dec 8, 2015 at 1:54 AM, Borislav Petkov <bp@alien8.de> wrote:
> On Mon, Dec 07, 2015 at 01:51:27PM -0800, Andy Lutomirski wrote:
>> This checks that ELF binaries are started with an appropriately
>> blank register state.
>>
>> (There's currently a nasty special case in the entry asm to arrange
>>  for this.  I'm planning on removing the special case, and this will
>>  help make sure I don't break it.)
>>
>> Signed-off-by: Andy Lutomirski <luto@kernel.org>
>> ---
>>  tools/testing/selftests/x86/Makefile               |   8 +-
>>  .../selftests/x86/check_initial_reg_state.c        | 108 +++++++++++++++++++++
>>  2 files changed, 115 insertions(+), 1 deletion(-)
>>  create mode 100644 tools/testing/selftests/x86/check_initial_reg_state.c
>>
>> diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
>> index a460fe7c5365..b82409421fa6 100644
>> --- a/tools/testing/selftests/x86/Makefile
>> +++ b/tools/testing/selftests/x86/Makefile
>> @@ -4,7 +4,7 @@ include ../lib.mk
>>
>>  .PHONY: all all_32 all_64 warn_32bit_failure clean
>>
>> -TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall
>> +TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs ldt_gdt syscall_nt ptrace_syscall check_initial_reg_state
>>  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault sigreturn test_syscall_vdso unwind_vdso
>>
>>  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
>> @@ -63,3 +63,9 @@ endif
>>  sysret_ss_attrs_64: thunks.S
>>  ptrace_syscall_32: raw_syscall_helper_32.S
>>  test_syscall_vdso_32: thunks_32.S
>> +
>> +# check_initial_reg_state is special: it needs a custom entry, and it
>> +# needs to be static so that its interpreter doesn't destroy its initial
>> +# state.
>> +check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
>> +check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
>> diff --git a/tools/testing/selftests/x86/check_initial_reg_state.c b/tools/testing/selftests/x86/check_initial_reg_state.c
>> new file mode 100644
>> index 000000000000..0cb565f7786d
>> --- /dev/null
>> +++ b/tools/testing/selftests/x86/check_initial_reg_state.c
>> @@ -0,0 +1,108 @@
>> +/*
>> + * check_initial_reg_state.c - check that execve sets the correct state
>> + * Copyright (c) 2014-2015 Andrew Lutomirski
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms and conditions of the GNU General Public License,
>> + * version 2, as published by the Free Software Foundation.
>> + *
>> + * This program is distributed in the hope it will be useful, but
>> + * WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * General Public License for more details.
>> + */
>> +
>> +#define _GNU_SOURCE
>> +
>> +#include <stdio.h>
>> +
>> +unsigned long ax, bx, cx, dx, si, di, bp, sp, flags;
>> +unsigned long r8, r9, r10, r11, r12, r13, r14, r15;
>> +
>> +asm (".pushsection .text\n\t"
>
> WARNING: please, no spaces at the start of a line
> #82: FILE: tools/testing/selftests/x86/check_initial_reg_state.c:23:
> +     ".type real_start, @function\n\t"$
>
> Something trampled over the preceding tabs in that whole asm().

That was intentional -- everything lines up with the top-level "asm(".
checkpatch is confused because it understands "\t " at the front of a
line but not just a space.

That being said, I could easily be convinced to switch to tabs there.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1287746

FromBorislav Petkov <bp@alien8.de>
Date2015-12-09 20:10 +0100
Message-ID<qDShH-3Zb-7@gated-at.bofh.it>
In reply to#1287741
On Wed, Dec 09, 2015 at 10:56:36AM -0800, Andy Lutomirski wrote:
> That was intentional -- everything lines up with the top-level "asm(".
> checkpatch is confused because it understands "\t " at the front of a
> line but not just a space.
> 
> That being said, I could easily be convinced to switch to tabs there.

I'd say kernel coding style is tabs of 8 chars...

You could do

    asm(".pushsection .text\n\t"
	".type real_start, @function\n\t"
	".global real_start\n\t"
	"real_start:\n\t"
#ifdef __x86_64__
	"mov %rax, ax\n\t"
	"mov %rbx, bx\n\t"
	...

and align the opening "asm(" to the first tab...

Bah, whatever, I'm not that finicky to care enough.

-- 
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1287760

FromBorislav Petkov <bp@alien8.de>
Date2015-12-09 20:30 +0100
Message-ID<qDSB4-460-15@gated-at.bofh.it>
In reply to#1287746
On Wed, Dec 09, 2015 at 11:20:53AM -0800, Andy Lutomirski wrote:
> emacs, the One True Reference Of Kernel Style, disagrees with you :)

Not me, my friend - the bible: Documentation/CodingStyle

:-P

-- 
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1287768

FromAndy Lutomirski <luto@amacapital.net>
Date2015-12-09 20:30 +0100
Message-ID<qDSB4-460-17@gated-at.bofh.it>
In reply to#1287746
On Wed, Dec 9, 2015 at 11:09 AM, Borislav Petkov <bp@alien8.de> wrote:
> On Wed, Dec 09, 2015 at 10:56:36AM -0800, Andy Lutomirski wrote:
>> That was intentional -- everything lines up with the top-level "asm(".
>> checkpatch is confused because it understands "\t " at the front of a
>> line but not just a space.
>>
>> That being said, I could easily be convinced to switch to tabs there.
>
> I'd say kernel coding style is tabs of 8 chars...

emacs, the One True Reference Of Kernel Style, disagrees with you :)

I'll change it.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web