// SPDX-License-Identifier: GPL-2.0
/****************************************************************************/
/*
* 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 <linux/coredump.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
#include <asm/cacheflush.h>
#include <asm/page.h>
#include <asm/flat.h>
#ifndef flat_get_relocate_addr
#define flat_get_relocate_addr(rel) (rel)
#endif
/****************************************************************************/
/*
* 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 */
#ifdef CONFIG_BINFMT_SHARED_FLAT
#define MAX_SHARED_LIBS (4)
#else
#define MAX_SHARED_LIBS (1)
#endif
#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
#define DATA_START_OFFSET_WORDS (0)
#else
#define DATA_START_OFFSET_WORDS (MAX_SHARED_LIBS)
#endif
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 *);
#ifdef CONFIG_COREDUMP
static int flat_core_dump(struct coredump_params *cprm);
#endif
static struct linux_binfmt flat_format = {
.module = THIS_MODULE,
.load_binary = load_flat_binary,
#ifdef CONFIG_COREDUMP
.core_dump = flat_core_dump,
.min_coredump = PAGE_SIZE
#endif
};
/****************************************************************************/
/*
* Routine writes a core dump image in the current directory.
* Currently only a stub-function.
*/
#ifdef CONFIG_COREDUMP
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;
}
#endif
/****************************************************************************/
/*
* 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;
if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK))
sp -= 2; /* argvp + envp */
sp -= 1; /* &argc */
current->mm->start_stack = (unsigned long)sp & -FLAT_STACK_ALIGN;
sp = (unsigned long __user *)current->mm->start_stack;
if (put_user(bprm->argc, sp++))
return -EFAULT;
if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK)) {
unsigned long argv, envp;
argv = (unsigned long)(sp + 2);
envp = (unsigned long)(sp + 2 + bprm->argc + 1);
if (put_user(argv, sp++) || put_user(envp, sp++))
return -EFAULT;
}
current->mm->arg_start = (unsigned long)p;
for (i = bprm->argc; i > 0; i--) {
if (put_user((unsigned long)p, sp++))
return -EFAULT;
len = strnlen_user(p, MAX_ARG_STRLEN);
if (!len || len > MAX_ARG_STRLEN)
return -EINVAL;
p += len;
}
if (put_user(0, sp++))
return -EFAULT;
current->mm->arg_end = (unsigned long)p;
current->mm->env_start = (unsigned long) p;
for (i = bprm->envc; i > 0; i--) {
if (put_user((unsigned long)p, sp++))
return -EFAULT;
len = strnlen_user(p, MAX_ARG_STRLEN);