#include <sys/param.h>
#include <sys/exec.h>
#include <sys/imgact.h>
#include <sys/imgact_elf.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/procfs.h>
#include <sys/reg.h>
#include <sys/sbuf.h>
#include <sys/sysent.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <machine/elf.h>
#ifdef COMPAT_LINUX32
#define linux_pt_regset linux_pt_regset32
#define bsd_to_linux_regset bsd_to_linux_regset32
#include <machine/../linux32/linux.h>
#else
#include <machine/../linux/linux.h>
#endif
#include <compat/linux/linux_elf.h>
#include <compat/linux/linux_mib.h>
#include <compat/linux/linux_misc.h>
struct l_elf_siginfo {
l_int si_signo;
l_int si_code;
l_int si_errno;
};
typedef struct linux_pt_regset l_elf_gregset_t;
struct linux_elf_prstatus {
struct l_elf_siginfo pr_info;
l_short pr_cursig;
l_ulong pr_sigpend;
l_ulong pr_sighold;
l_pid_t pr_pid;
l_pid_t pr_ppid;
l_pid_t pr_pgrp;
l_pid_t pr_sid;
l_timeval pr_utime;
l_timeval pr_stime;
l_timeval pr_cutime;
l_timeval pr_cstime;
l_elf_gregset_t pr_reg;
l_int pr_fpvalid;
};
#define LINUX_NT_AUXV 6
static void __linuxN(note_fpregset)(void *, struct sbuf *, size_t *);
static void __linuxN(note_prpsinfo)(void *, struct sbuf *, size_t *);
static void __linuxN(note_prstatus)(void *, struct sbuf *, size_t *);
static void __linuxN(note_threadmd)(void *, struct sbuf *, size_t *);
static void __linuxN(note_nt_auxv)(void *, struct sbuf *, size_t *);
void
__linuxN(prepare_notes)(struct thread *td, struct note_info_list *list,
size_t *sizep)
{
struct proc *p;
struct thread *thr;
size_t size;
p = td->td_proc;
size = 0;
thr = td;
while (thr != NULL) {
size += __elfN(register_note)(td, list,
NT_PRSTATUS, __linuxN(note_prstatus), thr);
size += __elfN(register_note)(td, list,
NT_PRPSINFO, __linuxN(note_prpsinfo), p);
size += __elfN(register_note)(td, list,
LINUX_NT_AUXV, __linuxN(note_nt_auxv), p);
size += __elfN(register_note)(td, list,
NT_FPREGSET, __linuxN(note_fpregset), thr);
size += __elfN(register_note)(td, list,
-1, __linuxN(note_threadmd), thr);
thr = thr == td ? TAILQ_FIRST(&p->p_threads) :
TAILQ_NEXT(thr, td_plist);
if (thr == td)
thr = TAILQ_NEXT(thr, td_plist);
}
*sizep = size;
}
typedef struct linux_elf_prstatus linux_elf_prstatus_t;
#ifdef COMPAT_LINUX32
typedef struct prpsinfo32 linux_elf_prpsinfo_t;
typedef struct fpreg32 linux_elf_prfpregset_t;
#else
typedef prpsinfo_t linux_elf_prpsinfo_t;
typedef prfpregset_t linux_elf_prfpregset_t;
#endif
static void
__linuxN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
{
struct sbuf sbarg;
size_t len;
char *cp, *end;
struct proc *p;
linux_elf_prpsinfo_t *psinfo;
int error;
p = arg;
if (sb != NULL) {
KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
psinfo->pr_version = PRPSINFO_VERSION;
psinfo->pr_psinfosz = sizeof(linux_elf_prpsinfo_t);
strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
PROC_LOCK(p);
if (p->p_args != NULL) {
len = sizeof(psinfo->pr_psargs) - 1;
if (len > p->p_args->ar_length)
len = p->p_args->ar_length;
memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
PROC_UNLOCK(p);
error = 0;
} else {
_PHOLD(p);
PROC_UNLOCK(p);
sbuf_new(&sbarg, psinfo->pr_psargs,
sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
error = proc_getargv(curthread, p, &sbarg);
PRELE(p);
if (sbuf_finish(&sbarg) == 0) {
len = sbuf_len(&sbarg) - 1;
if (len > 0)
len--;
} else {
len = sizeof(psinfo->pr_psargs) - 1;
}
sbuf_delete(&sbarg);
}
if (error != 0 || len == 0 || (ssize_t)len == -1)
strlcpy(psinfo->pr_psargs, p->p_comm,
sizeof(psinfo->pr_psargs));
else {
KASSERT(len < sizeof(psinfo->pr_psargs),
("len is too long: %zu vs %zu", len,
sizeof(psinfo->pr_psargs)));
cp = psinfo->pr_psargs;
end = cp + len - 1;
for (;;) {
cp = memchr(cp, '\0', end - cp);
if (cp == NULL)
break;
*cp = ' ';
}
}
psinfo->pr_pid = p->p_pid;
sbuf_bcat(sb, psinfo, sizeof(*psinfo));
free(psinfo, M_TEMP);
}
*sizep = sizeof(*psinfo);
}
static void
__linuxN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
{
struct thread *td;
linux_elf_prstatus_t *status;
#ifdef COMPAT_LINUX32
struct reg32 pr_reg;
#else
struct reg pr_reg;
#endif
td = arg;
if (sb != NULL) {
KASSERT(*sizep == sizeof(*status), ("invalid size"));
status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
status->pr_cursig = td->td_proc->p_sig;
status->pr_pid = td->td_tid;
#ifdef COMPAT_LINUX32
fill_regs32(td, &pr_reg);
#else
fill_regs(td, &pr_reg);
#endif
bsd_to_linux_regset(&pr_reg, &status->pr_reg);
sbuf_bcat(sb, status, sizeof(*status));
free(status, M_TEMP);
}
*sizep = sizeof(*status);
}
static void
__linuxN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
{
struct thread *td;
linux_elf_prfpregset_t *fpregset;
td = arg;
if (sb != NULL) {
KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
#ifdef COMPAT_LINUX32
fill_fpregs32(td, fpregset);
#else
fill_fpregs(td, fpregset);
#endif
sbuf_bcat(sb, fpregset, sizeof(*fpregset));
free(fpregset, M_TEMP);
}
*sizep = sizeof(*fpregset);
}
static void
__linuxN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
{
struct thread *td;
void *buf;
size_t size;
td = arg;
size = *sizep;
if (size != 0 && sb != NULL)
buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
else
buf = NULL;
size = 0;
__elfN(dump_thread)(td, buf, &size);
KASSERT(sb == NULL || *sizep == size, ("invalid size"));
if (size != 0 && sb != NULL)
sbuf_bcat(sb, buf, size);
free(buf, M_TEMP);
*sizep = size;
}
static void
__linuxN(note_nt_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
{
struct proc *p;
size_t size;
p = arg;
if (sb == NULL) {
size = 0;
sb = sbuf_new(NULL, NULL, LINUX_AT_COUNT * sizeof(Elf_Auxinfo),
SBUF_FIXEDLEN);
sbuf_set_drain(sb, sbuf_count_drain, &size);
PHOLD(p);
proc_getauxv(curthread, p, sb);
PRELE(p);
sbuf_finish(sb);
sbuf_delete(sb);
*sizep = size;
} else {
PHOLD(p);
proc_getauxv(curthread, p, sb);
PRELE(p);
}
}
int
__linuxN(copyout_strings)(struct image_params *imgp, uintptr_t *stack_base)
{
char canary[LINUX_AT_RANDOM_LEN];
char **vectp;
char *stringp;
uintptr_t destp, ustringp;
struct ps_strings *arginfo;
struct proc *p;
size_t execpath_len;
int argc, envc;
int error;
p = imgp->proc;
destp = PROC_PS_STRINGS(p);
arginfo = imgp->ps_strings = (void *)destp;
if (imgp->execpath != NULL && imgp->auxargs != NULL) {
execpath_len = strlen(imgp->execpath) + 1;
destp -= execpath_len;
destp = rounddown2(destp, sizeof(void *));
imgp->execpathp = (void *)destp;
error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
if (error != 0)
return (error);
}
arc4rand(canary, sizeof(canary), 0);
destp -= sizeof(canary);
imgp->canary = (void *)destp;
error = copyout(canary, imgp->canary, sizeof(canary));
if (error != 0)
return (error);
imgp->canarylen = sizeof(canary);
destp -= ARG_MAX - imgp->args->stringspace;
destp = rounddown2(destp, sizeof(void *));
ustringp = destp;
if (imgp->auxargs) {
destp -= LINUX_AT_COUNT * sizeof(Elf_Auxinfo);
destp = rounddown2(destp, sizeof(void *));
}
vectp = (char **)destp;
vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
vectp = (char **)((((uintptr_t)vectp + 8) & ~0xF) - 8);
*stack_base = (uintptr_t)vectp;
stringp = imgp->args->begin_argv;
argc = imgp->args->argc;
envc = imgp->args->envc;
error = copyout(stringp, (void *)ustringp,
ARG_MAX - imgp->args->stringspace);
if (error != 0)
return (error);
imgp->argv = vectp;
if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
suword32(&arginfo->ps_nargvstr, argc) != 0)
return (EFAULT);
for (; argc > 0; --argc) {
if (suword(vectp++, ustringp) != 0)
return (EFAULT);
while (*stringp++ != 0)
ustringp++;
ustringp++;
}
if (suword(vectp++, 0) != 0)
return (EFAULT);
imgp->envv = vectp;
if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
suword32(&arginfo->ps_nenvstr, envc) != 0)
return (EFAULT);
for (; envc > 0; --envc) {
if (suword(vectp++, ustringp) != 0)
return (EFAULT);
while (*stringp++ != 0)
ustringp++;
ustringp++;
}
if (suword(vectp, 0) != 0)
return (EFAULT);
if (imgp->auxargs) {
vectp++;
error = imgp->sysent->sv_copyout_auxargs(imgp,
(uintptr_t)vectp);
if (error != 0)
return (error);
}
return (0);
}
bool
linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
{
const Elf32_Word *desc;
uintptr_t p;
p = (uintptr_t)(note + 1);
p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
desc = (const Elf32_Word *)p;
if (desc[0] != GNU_ABI_LINUX)
return (false);
*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
return (true);
}
int
__linuxN(copyout_auxargs)(struct image_params *imgp, uintptr_t base)
{
struct thread *td = curthread;
Elf_Auxargs *args;
Elf_Auxinfo *aarray, *pos;
struct proc *p;
int error, issetugid;
p = imgp->proc;
issetugid = p->p_flag & P_SUGID ? 1 : 0;
args = imgp->auxargs;
aarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
M_WAITOK | M_ZERO);
__linuxN(arch_copyout_auxargs)(imgp, &pos);
if (linux_kernver(td) >= LINUX_KERNVER(2,4,0))
AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
AUXARGS_ENTRY(pos, AT_BASE, args->base);
AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
if (linux_kernver(td) >= LINUX_KERNVER(2,6,30))
AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
if (linux_kernver(td) >= LINUX_KERNVER(2,6,26) && imgp->execpathp != 0)
AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
if (args->execfd != -1)
AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
if (linux_kernver(td) >= LINUX_KERNVER(5,13,0))
AUXARGS_ENTRY(pos, LINUX_AT_MINSIGSTKSZ,
imgp->sysent->sv_minsigstksz);
AUXARGS_ENTRY(pos, AT_NULL, 0);
free(imgp->auxargs, M_TEMP);
imgp->auxargs = NULL;
KASSERT(pos - aarray <= LINUX_AT_COUNT, ("Too many auxargs"));
error = copyout(aarray, PTRIN(base), sizeof(*aarray) * LINUX_AT_COUNT);
free(aarray, M_TEMP);
return (error);
}