1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  27 /*        All Rights Reserved   */
  28 
  29 /*
  30  * Copyright 2016, Joyent, Inc.
  31  */
  32 
  33 #ifndef _SYS_EXEC_H
  34 #define _SYS_EXEC_H
  35 
  36 #include <sys/systm.h>
  37 #include <vm/seg.h>
  38 #include <vm/seg_vn.h>
  39 #include <sys/model.h>
  40 #include <sys/uio.h>
  41 #include <sys/corectl.h>
  42 #include <sys/machelf.h>
  43 
  44 #ifdef  __cplusplus
  45 extern "C" {
  46 #endif
  47 
  48 /*
  49  * Number of bytes to read for magic string
  50  */
  51 #define MAGIC_BYTES     8
  52 
  53 #define getexmag(x)     (((x)[0] << 8) + (x)[1])
  54 
  55 typedef struct execa {
  56         const char *fname;
  57         const char **argp;
  58         const char **envp;
  59 } execa_t;
  60 
  61 typedef struct execenv {
  62         caddr_t ex_bssbase;
  63         caddr_t ex_brkbase;
  64         size_t  ex_brksize;
  65         vnode_t *ex_vp;
  66         short   ex_magic;
  67 } execenv_t;
  68 
  69 #ifdef _KERNEL
  70 
  71 #define LOADABLE_EXEC(e)        ((e)->exec_lock)
  72 #define LOADED_EXEC(e)          ((e)->exec_func)
  73 
  74 
  75 /*
  76  * User argument structure for passing exec information around between the
  77  * common and machine-dependent portions of exec and the exec modules.
  78  */
  79 typedef struct uarg {
  80         ssize_t na;
  81         ssize_t ne;
  82         ssize_t nc;
  83         ssize_t arglen;
  84         char    *fname;
  85         char    *pathname;
  86         ssize_t auxsize;
  87         caddr_t stackend;
  88         size_t  stk_align;
  89         size_t  stk_size;
  90         char    *stk_base;
  91         char    *stk_strp;
  92         int     *stk_offp;
  93         size_t  usrstack_size;
  94         uint_t  stk_prot;
  95         uint_t  dat_prot;
  96         int     traceinval;
  97         int     addr32;
  98         model_t to_model;
  99         model_t from_model;
 100         size_t  to_ptrsize;
 101         size_t  from_ptrsize;
 102         size_t  ncargs;
 103         struct execsw *execswp;
 104         uintptr_t entry;
 105         uintptr_t thrptr;
 106         vnode_t *ex_vp;
 107         char    *emulator;
 108         char    *brandname;
 109         const char *brand_nroot;
 110         char    *auxp_auxflags; /* addr of auxflags auxv on the user stack */
 111         char    *auxp_brand; /* address of first brand auxv on user stack */
 112         cred_t  *pfcred;
 113         boolean_t scrubenv;
 114         uintptr_t maxstack;
 115         boolean_t stk_prot_override;
 116         uintptr_t commpage;
 117 } uarg_t;
 118 
 119 /*
 120  * Possible brand actions for exec.
 121  */
 122 #define EBA_NONE        0
 123 #define EBA_NATIVE      1
 124 #define EBA_BRAND       2
 125 
 126 /*
 127  * The following macro is a machine dependent encapsulation of
 128  * postfix processing to hide the stack direction from elf.c
 129  * thereby making the elf.c code machine independent.
 130  */
 131 #define execpoststack(ARGS, ARRAYADDR, BYTESIZE) \
 132         (copyout((caddr_t)(ARRAYADDR), (ARGS)->stackend, (BYTESIZE)) ? EFAULT \
 133                 : (((ARGS)->stackend += (BYTESIZE)), 0))
 134 
 135 /*
 136  * This provides the current user stack address for an object of size BYTESIZE.
 137  * Used to determine the stack address just before applying execpoststack().
 138  */
 139 #define stackaddress(ARGS, BYTESIZE)    ((ARGS)->stackend)
 140 
 141 /*
 142  * Macro to add attribute/values the aux vector under construction.
 143  */
 144 /* BEGIN CSTYLED */
 145 #if ((_LONG_ALIGNMENT == (2 * _INT_ALIGNMENT)) || \
 146      (_POINTER_ALIGNMENT == (2 * _INT_ALIGNMENT)))
 147 /* END CSTYLED */
 148 /*
 149  * This convoluted stuff is necessitated by the fact that there is
 150  * potential padding in the aux vector, but not necessarily and
 151  * without clearing the padding there is a small, but potential
 152  * security hole.
 153  */
 154 #define ADDAUX(p, a, v) {               \
 155                 (&(p)->a_type)[1] = 0;   \
 156                 (p)->a_type = (a);   \
 157                 (p)->a_un.a_val = (v);       \
 158                 ++(p);                  \
 159         }
 160 #else
 161 #define ADDAUX(p, a, v) {                       \
 162                 (p)->a_type = (a);           \
 163                 ((p)++)->a_un.a_val = (v);   \
 164         }
 165 #endif
 166 
 167 #define INTPSZ  MAXPATHLEN
 168 #define INTP_MAXDEPTH   5       /* Nested interpreter depth matches Linux */
 169 typedef struct intpdata {
 170         char    *intp;
 171         char    *intp_name[INTP_MAXDEPTH];
 172         char    *intp_arg[INTP_MAXDEPTH];
 173 } intpdata_t;
 174 
 175 #define EXECSETID_SETID         0x1 /* setid exec */
 176 #define EXECSETID_UGIDS         0x2 /* [ug]ids mismatch */
 177 #define EXECSETID_PRIVS         0x4 /* more privs than before */
 178 
 179 struct execsw {
 180         char    *exec_magic;
 181         int     exec_magoff;
 182         int     exec_maglen;
 183         int     (*exec_func)(struct vnode *vp, struct execa *uap,
 184                     struct uarg *args, struct intpdata *idata, int level,
 185                     long *execsz, int setid, caddr_t exec_file,
 186                     struct cred *cred, int *brand_action);
 187         int     (*exec_core)(struct vnode *vp, struct proc *p,
 188                     struct cred *cred, rlim64_t rlimit, int sig,
 189                     core_content_t content);
 190         krwlock_t       *exec_lock;
 191 };
 192 
 193 extern int nexectype;           /* number of elements in execsw */
 194 extern struct execsw execsw[];
 195 extern kmutex_t execsw_lock;
 196 
 197 extern short elfmagic;
 198 extern short intpmagic;
 199 extern short javamagic;
 200 #if defined(__sparc)
 201 extern short aout_zmagic;
 202 extern short aout_nmagic;
 203 extern short aout_omagic;
 204 #endif
 205 extern short nomagic;
 206 
 207 extern char elf32magicstr[];
 208 extern char elf64magicstr[];
 209 extern char intpmagicstr[];
 210 extern char javamagicstr[];
 211 #if defined(__sparc)
 212 extern char aout_nmagicstr[];
 213 extern char aout_zmagicstr[];
 214 extern char aout_omagicstr[];
 215 #endif
 216 extern char nomagicstr[];
 217 
 218 extern int exec_args(execa_t *, uarg_t *, intpdata_t *, void **);
 219 extern int exece(const char *fname, const char **argp, const char **envp);
 220 extern int exec_common(const char *fname, const char **argp,
 221     const char **envp, int brand_action);
 222 extern int gexec(vnode_t **vp, struct execa *uap, struct uarg *args,
 223     struct intpdata *idata, int level, long *execsz, caddr_t exec_file,
 224     struct cred *cred, int *brand_action);
 225 extern struct execsw *allocate_execsw(char *name, char *magic,
 226     size_t magic_size);
 227 extern struct execsw *findexecsw(char *magic);
 228 extern struct execsw *findexec_by_hdr(char *header);
 229 extern struct execsw *findexec_by_magic(char *magic);
 230 extern int execpermissions(struct vnode *vp, struct vattr *vattrp,
 231     struct uarg *args);
 232 extern int execmap(vnode_t *vp, caddr_t addr, size_t len, size_t zfodlen,
 233     off_t offset, int prot, int page, uint_t);
 234 extern void setexecenv(struct execenv *ep);
 235 extern int execopen(struct vnode **vpp, int *fdp);
 236 extern int execclose(int fd);
 237 extern void setregs(uarg_t *);
 238 extern void exec_set_sp(size_t);
 239 
 240 /*
 241  * Utility functions for branded process executing
 242  */
 243 #if !defined(_ELF32_COMPAT)
 244 /*
 245  * When compiling 64-bit kernels we don't want these definitions included
 246  * when compiling the 32-bit compatability elf code in the elfexec module.
 247  */
 248 extern int elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
 249     long *, int, caddr_t, cred_t *, int *);
 250 extern int mapexec_brand(vnode_t *, uarg_t *, Ehdr *, Addr *,
 251     intptr_t *, caddr_t, char **, caddr_t *, caddr_t *, size_t *,
 252     uintptr_t *, uintptr_t *);
 253 extern int elfreadhdr(vnode_t *, cred_t *, Ehdr *, int *, caddr_t *,
 254     ssize_t *);
 255 #endif /* !_ELF32_COMPAT */
 256 
 257 #if defined(_LP64)
 258 extern int elf32exec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
 259     long *, int, caddr_t, cred_t *, int *);
 260 extern int mapexec32_brand(vnode_t *, uarg_t *, Elf32_Ehdr *, Elf32_Addr *,
 261     intptr_t *, caddr_t, char **, caddr_t *, caddr_t *, size_t *,
 262     uintptr_t *, uintptr_t *);
 263 extern int elf32readhdr(vnode_t *, cred_t *, Elf32_Ehdr *, int *, caddr_t *,
 264     ssize_t *);
 265 #endif  /* _LP64 */
 266 
 267 /*
 268  * Utility functions for exec module core routines:
 269  */
 270 extern int core_seg(proc_t *, vnode_t *, offset_t, caddr_t,
 271     size_t, rlim64_t, cred_t *);
 272 
 273 extern int core_write(vnode_t *, enum uio_seg, offset_t,
 274     const void *, size_t, rlim64_t, cred_t *);
 275 
 276 /* a.out stuff */
 277 
 278 struct exec;
 279 
 280 extern caddr_t gettmem(struct exec *exp);
 281 extern caddr_t getdmem(struct exec *exp);
 282 extern ulong_t getdfile(struct exec *exp);
 283 extern uint_t gettfile(struct exec *exp);
 284 extern int chkaout(struct exdata *exp);
 285 extern void getexinfo(struct exdata *edp_in, struct exdata *edp_out,
 286     int *pagetext, int *pagedata);
 287 
 288 #endif  /* _KERNEL */
 289 
 290 #ifdef  __cplusplus
 291 }
 292 #endif
 293 
 294 #endif /* _SYS_EXEC_H */