Skip to contentSkip to conversation

binfmt_misc: pass info about P flag by AT_FLAGS

PATCH Superseded YunQiang Su 2 participants 4 messages Jan 16, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 binfmt_misc: pass info about P flag by AT_FLAGS

From: YunQiang Su <[email protected]>

Currently program invoked by binfmt_misc cannot be aware about whether P flag, aka preserve path is enabled.

Some applications like qemu need to know since it has 2 use case:
1. call by hand, like: qemu-mipsel-static test.app OPTION so, qemu have to assume that P option is not enabled.
2. call by binfmt_misc. If qemu cannot know about whether P flag is enabled, distribution's have to set qemu without P flag, and binfmt_misc call qemu like:
qemu-mipsel-static /absolute/path/to/test.app OPTION
even test.app is not called by absoulute path, like
./relative/path/to/test.app

This patch passes this information by the 3rd bits of unused AT_FLAGS. Then, in qemu, we can get this info by:
getauxval(AT_FLAGS) & (1<<3)

v1->v2:
not enable kdebug

See: https://bugs.launchpad.net/qemu/+bug/1818483

---
 fs/binfmt_elf.c         | 6 +++++-
 fs/binfmt_elf_fdpic.c   | 6 +++++-
 fs/binfmt_misc.c        | 2 ++
 include/linux/binfmts.h | 4 ++++
 4 files changed, 16 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +5 -1
index f4713ea76e82..d33ee07d7f57 100644
@@ -178,6 +178,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	unsigned char k_rand_bytes[16];
 	int items;
 	elf_addr_t *elf_info;
+	elf_addr_t flags = 0;
 	int ei_index;
 	const struct cred *cred = current_cred();
 	struct vm_area_struct *vma;
@@ -252,7 +253,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
 	NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
 	NEW_AUX_ENT(AT_BASE, interp_load_addr);
-	NEW_AUX_ENT(AT_FLAGS, 0);
+	if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0) {
+		flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
+	}
+	NEW_AUX_ENT(AT_FLAGS, flags);
 	NEW_AUX_ENT(AT_ENTRY, e_entry);
 	NEW_AUX_ENT(AT_UID, from_kuid_munged(cred->user_ns, cred->uid));
 	NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
fs/binfmt_elf_fdpic.c +5 -1
index 240f66663543..5ad8fdb3babe 100644
@@ -507,6 +507,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	char __user *u_platform, *u_base_platform, *p;
 	int loop;
 	int nr;	/* reset for each csp adjustment */
+	unsigned long flags = 0;
 
 #ifdef CONFIG_MMU
 	/* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
@@ -647,7 +648,10 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	NEW_AUX_ENT(AT_PHENT,	sizeof(struct elf_phdr));
 	NEW_AUX_ENT(AT_PHNUM,	exec_params->hdr.e_phnum);
 	NEW_AUX_ENT(AT_BASE,	interp_params->elfhdr_addr);
-	NEW_AUX_ENT(AT_FLAGS,	0);
+	if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0) {
+		flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
+	}
+	NEW_AUX_ENT(AT_FLAGS,	flags);
 	NEW_AUX_ENT(AT_ENTRY,	exec_params->entry_addr);
 	NEW_AUX_ENT(AT_UID,	(elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
 	NEW_AUX_ENT(AT_EUID,	(elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
fs/binfmt_misc.c +2 -0
index cdb45829354d..3f41b667b241 100644
@@ -158,6 +158,8 @@ static int load_misc_binary(struct linux_binprm *bprm)
 		retval = remove_arg_zero(bprm);
 		if (retval)
 			goto ret;
+	} else {
+		bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
 	}
 
 	if (fmt->flags & MISC_FMT_OPEN_BINARY) {
include/linux/binfmts.h +4 -0
index b40fc633f3be..265b80d5fd6f 100644
@@ -78,6 +78,10 @@ struct linux_binprm {
 #define BINPRM_FLAGS_PATH_INACCESSIBLE_BIT 2
 #define BINPRM_FLAGS_PATH_INACCESSIBLE (1 << BINPRM_FLAGS_PATH_INACCESSIBLE_BIT)
 
+/* if preserve the argv0 for the interpreter  */
+#define BINPRM_FLAGS_PRESERVE_ARGV0_BIT 3
+#define BINPRM_FLAGS_PRESERVE_ARGV0 (1 << BINPRM_FLAGS_PRESERVE_ARGV0_BIT)
+
 /* Function parameter for binfmt->coredump */
 struct coredump_params {
 	const kernel_siginfo_t *siginfo;
Signature · 1 line
2.25.0.rc1
#1 lore raw

Le 16/01/2020 à 03:20, YunQiang Su a écrit :

Quoted 44 lines

From: YunQiang Su <[email protected]>

Currently program invoked by binfmt_misc cannot be aware about whether P flag, aka preserve path is enabled.

Some applications like qemu need to know since it has 2 use case:
1. call by hand, like: qemu-mipsel-static test.app OPTION so, qemu have to assume that P option is not enabled.
2. call by binfmt_misc. If qemu cannot know about whether P flag is enabled, distribution's have to set qemu without P flag, and binfmt_misc call qemu like:
qemu-mipsel-static /absolute/path/to/test.app OPTION
even test.app is not called by absoulute path, like
./relative/path/to/test.app

This patch passes this information by the 3rd bits of unused AT_FLAGS. Then, in qemu, we can get this info by:

   getauxval(AT_FLAGS) & (1<<3)

v1->v2:
not enable kdebug

See: https://bugs.launchpad.net/qemu/+bug/1818483

---
 fs/binfmt_elf.c         | 6 +++++-
 fs/binfmt_elf_fdpic.c   | 6 +++++-
 fs/binfmt_misc.c        | 2 ++
 include/linux/binfmts.h | 4 ++++
 4 files changed, 16 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +1 -0
index f4713ea76e82..d33ee07d7f57 100644
@@ -178,6 +178,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	unsigned char k_rand_bytes[16];
 	int items;
 	elf_addr_t *elf_info;
+	elf_addr_t flags = 0;
 	int ei_index;
 	const struct cred *cred = current_cred();
 	struct vm_area_struct *vma;

fs/binfmt_elf.c create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, · line 258

Quoted 7 lines
fs/binfmt_elf.c +3 -1
@@ -252,7 +253,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
 	NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
 	NEW_AUX_ENT(AT_BASE, interp_load_addr);
-	NEW_AUX_ENT(AT_FLAGS, 0);
+	if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0) {
+		flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
+	}

Perhaps we also need a different flag in AT_FLAG than in interp_flag as BINPRM_FLAGS_PRESERVE_ARGV0 is also part of the internal ABI?

Al?

Thanks,
Laurent

#2 lore raw
Quoted 48 lines · Laurent Vivier <[email protected]> 于2020年1月16日周四 下午4:07写道:
Quoted 46 lines · YunQiang Su

From: YunQiang Su <[email protected]>

Currently program invoked by binfmt_misc cannot be aware about whether P flag, aka preserve path is enabled.

Some applications like qemu need to know since it has 2 use case:
1. call by hand, like: qemu-mipsel-static test.app OPTION so, qemu have to assume that P option is not enabled.
2. call by binfmt_misc. If qemu cannot know about whether P flag is enabled, distribution's have to set qemu without P flag, and binfmt_misc call qemu like:
qemu-mipsel-static /absolute/path/to/test.app OPTION
even test.app is not called by absoulute path, like
./relative/path/to/test.app

This patch passes this information by the 3rd bits of unused AT_FLAGS. Then, in qemu, we can get this info by:

   getauxval(AT_FLAGS) & (1<<3)

v1->v2:
not enable kdebug

See: https://bugs.launchpad.net/qemu/+bug/1818483

---
 fs/binfmt_elf.c         | 6 +++++-
 fs/binfmt_elf_fdpic.c   | 6 +++++-
 fs/binfmt_misc.c        | 2 ++
 include/linux/binfmts.h | 4 ++++
 4 files changed, 16 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +1 -0
index f4713ea76e82..d33ee07d7f57 100644
@@ -178,6 +178,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
      unsigned char k_rand_bytes[16];
      int items;
      elf_addr_t *elf_info;
+     elf_addr_t flags = 0;
      int ei_index;
      const struct cred *cred = current_cred();
      struct vm_area_struct *vma;
@@ -252,7 +253,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
      NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
      NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
Quoted 8 lines

fs/binfmt_elf.c create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, · line 258

Quoted 5 lines
fs/binfmt_elf.c +3 -1
      NEW_AUX_ENT(AT_BASE, interp_load_addr);
-     NEW_AUX_ENT(AT_FLAGS, 0);
+     if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0) {
+             flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
+     }

Perhaps we also need a different flag in AT_FLAG than in interp_flag as BINPRM_FLAGS_PRESERVE_ARGV0 is also part of the internal ABI?

yep. It may be really a problem.
So, should we define a set of new macros for AT_FLAGS?

Quoted 6 lines

Al?

Thanks,
Laurent

#3 lore raw

Le 16/01/2020 à 09:17, YunQiang Su a écrit :

Quoted 58 lines
Quoted 48 lines · Laurent Vivier <[email protected]> 于2020年1月16日周四 下午4:07写道:
Quoted 46 lines · YunQiang Su

From: YunQiang Su <[email protected]>

Currently program invoked by binfmt_misc cannot be aware about whether P flag, aka preserve path is enabled.

Some applications like qemu need to know since it has 2 use case:
1. call by hand, like: qemu-mipsel-static test.app OPTION so, qemu have to assume that P option is not enabled.
2. call by binfmt_misc. If qemu cannot know about whether P flag is enabled, distribution's have to set qemu without P flag, and binfmt_misc call qemu like:
qemu-mipsel-static /absolute/path/to/test.app OPTION
even test.app is not called by absoulute path, like
./relative/path/to/test.app

This patch passes this information by the 3rd bits of unused AT_FLAGS. Then, in qemu, we can get this info by:

   getauxval(AT_FLAGS) & (1<<3)

v1->v2:
not enable kdebug

See: https://bugs.launchpad.net/qemu/+bug/1818483

---
 fs/binfmt_elf.c         | 6 +++++-
 fs/binfmt_elf_fdpic.c   | 6 +++++-
 fs/binfmt_misc.c        | 2 ++
 include/linux/binfmts.h | 4 ++++
 4 files changed, 16 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +1 -0
index f4713ea76e82..d33ee07d7f57 100644
@@ -178,6 +178,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
      unsigned char k_rand_bytes[16];
      int items;
      elf_addr_t *elf_info;
+     elf_addr_t flags = 0;
      int ei_index;
      const struct cred *cred = current_cred();
      struct vm_area_struct *vma;
@@ -252,7 +253,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
      NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
      NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
Quoted 8 lines

fs/binfmt_elf.c create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, · line 258

Quoted 5 lines
fs/binfmt_elf.c +3 -1
      NEW_AUX_ENT(AT_BASE, interp_load_addr);
-     NEW_AUX_ENT(AT_FLAGS, 0);
+     if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0) {
+             flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
+     }

Perhaps we also need a different flag in AT_FLAG than in interp_flag as BINPRM_FLAGS_PRESERVE_ARGV0 is also part of the internal ABI?

Quoted 2 lines

yep. It may be really a problem.
So, should we define a set of new macros for AT_FLAGS?

Yes, I think.

Thanks,
Laurent