summaryrefslogtreecommitdiff
path: root/libdiskfs/file-exec.c
blob: ec9bc424b28ac6cecdec495da62a524df2d52e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* File execution (file_exec_paths RPC) for diskfs servers, using exec server.
   Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002,
   2010 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   The GNU Hurd is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include "priv.h"
#include "fs_S.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <hurd/exec.h>
#include <hurd/paths.h>
#include <string.h>
#include <idvec.h>

kern_return_t
diskfs_S_file_exec (struct protid *cred,
		    task_t task,
		    int flags,
		    const_data_t argv,
		    size_t argvlen,
		    const_data_t envp,
		    size_t envplen,
		    const mach_port_t *fds,
		    size_t fdslen,
		    const mach_port_t *portarray,
		    size_t portarraylen,
		    const int *intarray,
		    size_t intarraylen,
		    const mach_port_t *deallocnames,
		    size_t deallocnameslen,
		    const mach_port_t *destroynames,
		    size_t destroynameslen)
{
  return diskfs_S_file_exec_paths (cred,
				   task,
				   flags,
				   "",
				   "",
				   argv, argvlen,
				   envp, envplen,
				   fds, fdslen,
				   portarray, portarraylen,
				   intarray, intarraylen,
				   deallocnames, deallocnameslen,
				   destroynames, destroynameslen);
}

kern_return_t
diskfs_S_file_exec_paths (struct protid *cred,
			  task_t task,
			  int flags,
			  const_string_t path,
			  const_string_t abspath,
			  const_data_t argv,
			  size_t argvlen,
			  const_data_t envp,
			  size_t envplen,
			  const mach_port_t *fds,
			  size_t fdslen,
			  const mach_port_t *portarray,
			  size_t portarraylen,
			  const int *intarray,
			  size_t intarraylen,
			  const mach_port_t *deallocnames,
			  size_t deallocnameslen,
			  const mach_port_t *destroynames,
			  size_t destroynameslen)
{
  struct node *np;
  uid_t uid;
  gid_t gid;
  mode_t mode;
  int suid, sgid;
  struct protid *newpi;
  struct peropen *newpo;
  error_t err = 0;
  mach_port_t execserver;
  int cached_exec;
  struct hurd_userlink ulink;
  mach_port_t right, cred_right;

#define RETURN(code) do { err = (code); goto out; } while (0)

  if (!cred)
    return EOPNOTSUPP;

  /* Get a light reference to the cached exec server port.  */
  execserver = _hurd_port_get (&_diskfs_exec_portcell, &ulink);
  cached_exec = (execserver != MACH_PORT_NULL);
  if (execserver == MACH_PORT_NULL)
    {
      /* No cached port.  Look up the canonical naming point.  */
      execserver = file_name_lookup (_SERVERS_EXEC, 0, 0);
      if (execserver == MACH_PORT_NULL)
	return EOPNOTSUPP;	/* No exec server, no exec.  */
      else
	{
	  /* Install the newly-gotten exec server port for other
	     threads to use, then get a light reference for this call.  */
	  _hurd_port_set (&_diskfs_exec_portcell, execserver);
	  execserver = _hurd_port_get (&_diskfs_exec_portcell, &ulink);
	}
    }

  np = cred->po->np;

  pthread_mutex_lock (&np->lock);
  mode = np->dn_stat.st_mode;
  uid = np->dn_stat.st_uid;
  gid = np->dn_stat.st_gid;
  pthread_mutex_unlock (&np->lock);

  if (_diskfs_noexec)
    RETURN (EACCES);

  if ((cred->po->openstat & O_EXEC) == 0)
    RETURN (EBADF);

  if (!((mode & (S_IXUSR|S_IXGRP|S_IXOTH))
	|| ((mode & S_IUSEUNK) && (mode & (S_IEXEC << S_IUNKSHIFT)))))
    RETURN (EACCES);

  if ((mode & S_IFMT) == S_IFDIR)
    RETURN (EACCES);

  suid = mode & S_ISUID;
  sgid = mode & S_ISGID;
  if (!_diskfs_nosuid && (suid || sgid))
    {
      int secure = 0;
      error_t get_file_ids (struct idvec *uids, struct idvec *gids)
	{
	  error_t err = idvec_merge (uids, cred->user->uids);
	  if (! err)
	    err = idvec_merge (gids, cred->user->gids);
	  return err;
	}
      err =
	fshelp_exec_reauth (suid, uid, sgid, gid,
			    diskfs_auth_server_port, get_file_ids,
			    portarray, portarraylen, fds, fdslen, &secure);
      if (secure)
	flags |= EXEC_SECURE | EXEC_NEWTASK;
    }

  /* If the user can't read the file, then we should use a new task,
     which would be inaccessible to the user.  Actually, this doesn't
     work, because the proc server will still give out the task port
     to the user.  Too many things depend on that that it can't be
     changed.  So this vague attempt isn't even worth trying.  */
#if 0
  if (fshelp_access (&np->dn_stat, S_IREAD, cred->user))
    flags |= EXEC_NEWTASK;
#endif

  if (! err)
    /* Make a new peropen for the exec server to access the file, since any
       seeking the exec server might want to do should not affect the original
       peropen on which file_exec_paths was called.  (The new protid
       for this peropen clones the caller's iouser to preserve the caller's
       authentication credentials.)  The new peropen's openmodes must have
       O_READ even if the caller had only O_EXEC privilege, so the exec
       server can read the executable file.  We also include O_EXEC so that
       the exec server can turn this peropen into a file descriptor in the
       target process and permit it to exec its /dev/fd/N pseudo-file.  */
    {
      err = diskfs_make_peropen (np, O_READ|O_EXEC, cred->po, &newpo);
      if (! err)
	{
	  err = diskfs_create_protid (newpo, cred->user, &newpi);
	  if (err)
	    diskfs_release_peropen (newpo);
	}
    }

  if (! err)
    {
      do
	{
	  right = ports_get_send_right (newpi);

	  /* Add a gratuitous send right on the protid to avoid a no-sender, and thus
	     interrupt the exec, just because we are precisely replacing the calling
	     process!  */
	  cred_right = ports_get_send_right (cred);
#ifdef HAVE_EXEC_EXEC_PATHS
	  err = exec_exec_paths (execserver,
				 right, MACH_MSG_TYPE_COPY_SEND,
				 task, flags, path, abspath,
				 argv, argvlen, envp, envplen,
				 fds, MACH_MSG_TYPE_COPY_SEND, fdslen,
				 portarray, MACH_MSG_TYPE_COPY_SEND,
				 portarraylen,
				 intarray, intarraylen,
				 deallocnames, deallocnameslen,
				 destroynames, destroynameslen);
	  /* For backwards compatibility.  Just drop it when we kill
	     exec_exec.  */
	  if (err == MIG_BAD_ID)
#endif
	    err = exec_exec (execserver,
			     right, MACH_MSG_TYPE_COPY_SEND,
			     task, flags, argv, argvlen, envp, envplen,
			     fds, MACH_MSG_TYPE_COPY_SEND, fdslen,
			     portarray, MACH_MSG_TYPE_COPY_SEND, portarraylen,
			     intarray, intarraylen,
			     deallocnames, deallocnameslen,
			     destroynames, destroynameslen);


	  mach_port_deallocate (mach_task_self (), right);
	  mach_port_deallocate (mach_task_self (), cred_right);
	  if (err == MACH_SEND_INVALID_DEST)
	    {
	      if (cached_exec)
		{
		  /* We were using a previously looked-up exec server port.
		     Try looking up a new one before giving an error.  */
		  cached_exec = 0;
		  _hurd_port_free (&_diskfs_exec_portcell, &ulink, execserver);

		  execserver = file_name_lookup (_SERVERS_EXEC, 0, 0);
		  if (execserver == MACH_PORT_NULL)
		    err = EOPNOTSUPP;
		  else
		    {
		      _hurd_port_set (&_diskfs_exec_portcell, execserver);
		      execserver = _hurd_port_get (&_diskfs_exec_portcell,
						   &ulink);
		    }
		}
	      else
		err = EOPNOTSUPP;
	    }
	} while (err == MACH_SEND_INVALID_DEST);
      ports_port_deref (newpi);
    }

  if (! err)
    {
      unsigned int i;

      mach_port_deallocate (mach_task_self (), task);
      for (i = 0; i < fdslen; i++)
	mach_port_deallocate (mach_task_self (), fds[i]);
      for (i = 0; i < portarraylen; i++)
	mach_port_deallocate (mach_task_self (), portarray[i]);
    }

 out:
  _hurd_port_free (&_diskfs_exec_portcell, &ulink, execserver);

  return err;
}