/****************************************************************************/
/*
* linux/fs/binfmt_flat.c
*
* Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
* Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
* Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
* Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
* based heavily on:
*
* linux/fs/binfmt_aout.c:
* Copyright (C) 1991, 1992, 1996 Linus Torvalds
* linux/fs/binfmt_flat.c for 2.0 kernel
* Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
* JAN/99 -- coded full program relocation (gerg@snapgear.com)
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/sched/task_stack.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/user.h>
#include <linux/slab.h>
#include <linux/binfmts.h>
#include <linux/personality.h>
#include <linux/init.h>
#include <linux/flat.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
#include <asm/cacheflush.h>
#include <asm/page.h>
/****************************************************************************/
/*
* User data (data section and bss) needs to be aligned.
* We pick 0x20 here because it is the max value elf2flt has always
* used in producing FLAT files, and because it seems to be large
* enough to make all the gcc alignment related tests happy.
*/
#define FLAT_DATA_ALIGN (0x20)
/*
* User data (stack) also needs to be aligned.
* Here we can be a bit looser than the data sections since this
* needs to only meet arch ABI requirements.
*/
#define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN)
#define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */
#define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */
struct lib_info {
struct {
unsigned long start_code; /* Start of text segment */
unsigned long start_data; /* Start of data segment */
unsigned long start_brk; /* End of data segment */
unsigned long text_len; /* Length of text segment */
unsigned long entry; /* Start address for this module */
unsigned long build_date; /* When this one was compiled */
bool loaded; /* Has this library been loaded? */
} lib_list[MAX_SHARED_LIBS];
};
#ifdef CONFIG_BINFMT_SHARED_FLAT
static int load_flat_shared_library(int id, struct lib_info *p);
#endif
static int load_flat_binary(struct linux_binprm *);
static int flat_core_dump(struct coredump_params *cprm);
static struct linux_binfmt flat_format = {
.module = THIS_MODULE,
.load_binary = load_flat_binary,
.core_dump = flat_core_dump,
.min_coredump = PAGE_SIZE
};
/****************************************************************************/
/*
* Routine writes a core dump image in the current directory.
* Currently only a stub-function.
*/
static int flat_core_dump(struct coredump_params *cprm)
{
pr_warn("Process %s:%d received signr %d and should have core dumped\n",
current->comm, current->pid, cprm->siginfo->si_signo);
return 1;
}
/****************************************************************************/
/*
* create_flat_tables() parses the env- and arg-strings in new user
* memory and creates the pointer tables from them, and puts their
* addresses on the "stack", recording the new stack pointer value.
*/
static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start)
{
char __user *p;
unsigned long __user *sp;
long i, len;
p = (char __user *)arg_start;
sp = (unsigned long __user *)current->mm->start_stack;
sp -= bprm->envc + 1;
sp -= bprm->argc + 1;
sp -= flat_argvp_envp_on_stack() ? 2 : 0;
sp -= 1; /* &argc */
current->mm->start_stack = (unsigned long)sp & -FLAT_STACK_ALIGN;
sp = (unsigned long __user *)current->mm->start_stack;
__put_user(bprm->argc, sp++);
if (flat_argvp_envp_on_stack()) {
unsigned long argv, envp;
argv = (unsigned long)(sp + 2);
envp = (unsigned long)(sp + 2 + bprm->argc + 1);
__put_user(argv, sp++);
__put_user(envp, sp++);
}
current->mm->arg_start = (unsigned long)p;
for (i = bprm->argc; i > 0; i--) {
__put_user((unsigned long)p, sp++);
len = strnlen_user(p, MAX_ARG_STRLEN);
if (!len || len > MAX_ARG_STRLEN)
return -EINVAL;
p += len;
}
__put_user(0, sp++);
current->mm->arg_end = (unsigned long)p;
current->mm->env_start = (unsigned long) p;
for (i = bprm->envc; i > 0;