Module Name:    src
Committed By:   christos
Date:           Fri Apr 27 18:33:24 UTC 2018

Modified Files:
        src/sys/kern: exec_script.c kern_exec.c
        src/sys/sys: exec.h

Log Message:
Canonicalize the interpreter path in #! scripts since check_exec() expects
an absolute path, and we KASSERT if that's not the case later.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/sys/kern/exec_script.c
cvs rdiff -u -r1.456 -r1.457 src/sys/kern/kern_exec.c
cvs rdiff -u -r1.152 -r1.153 src/sys/sys/exec.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/exec_script.c
diff -u src/sys/kern/exec_script.c:1.74 src/sys/kern/exec_script.c:1.75
--- src/sys/kern/exec_script.c:1.74	Fri Sep  5 05:20:59 2014
+++ src/sys/kern/exec_script.c	Fri Apr 27 14:33:24 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_script.c,v 1.74 2014/09/05 09:20:59 matt Exp $	*/
+/*	$NetBSD: exec_script.c,v 1.75 2018/04/27 18:33:24 christos Exp $	*/
 
 /*
  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.74 2014/09/05 09:20:59 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.75 2018/04/27 18:33:24 christos Exp $");
 
 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
 #define FDSCRIPTS		/* Need this for safe set-id scripts. */
@@ -280,10 +280,8 @@ check_shell:
 	epp->ep_hdrvalid = 0;
 
 	/* try loading the interpreter */
-	shell_pathbuf = pathbuf_create(shellname);
-	if (shell_pathbuf == NULL) {
-		error = ENOMEM;
-	} else {
+	if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE,
+	    &shell_pathbuf, NULL)) == 0) {
 		error = check_exec(l, epp, shell_pathbuf);
 		pathbuf_destroy(shell_pathbuf);
 	}

Index: src/sys/kern/kern_exec.c
diff -u src/sys/kern/kern_exec.c:1.456 src/sys/kern/kern_exec.c:1.457
--- src/sys/kern/kern_exec.c:1.456	Fri Feb 23 14:43:08 2018
+++ src/sys/kern/kern_exec.c	Fri Apr 27 14:33:24 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exec.c,v 1.456 2018/02/23 19:43:08 maxv Exp $	*/
+/*	$NetBSD: kern_exec.c,v 1.457 2018/04/27 18:33:24 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.456 2018/02/23 19:43:08 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.457 2018/04/27 18:33:24 christos Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -607,9 +607,9 @@ exec_autoload(void)
 #endif
 }
 
-static int
-makepathbuf(struct lwp *l, const char *upath, struct pathbuf **pbp,
-    size_t *offs)
+int
+exec_makepathbuf(struct lwp *l, const char *upath, enum uio_seg seg,
+    struct pathbuf **pbp, size_t *offs)
 {
 	char *path, *bp;
 	size_t len, tlen;
@@ -617,7 +617,11 @@ makepathbuf(struct lwp *l, const char *u
 	struct cwdinfo *cwdi;
 
 	path = PNBUF_GET();
-	error = copyinstr(upath, path, MAXPATHLEN, &len);
+	if (seg == UIO_SYSSPACE) {
+		error = copystr(upath, path, MAXPATHLEN, &len);
+	} else {
+		error = copyinstr(upath, path, MAXPATHLEN, &len);
+	}
 	if (error) {
 		PNBUF_PUT(path);
 		DPRINTF(("%s: copyin path @%p %d\n", __func__, upath, error));
@@ -625,7 +629,8 @@ makepathbuf(struct lwp *l, const char *u
 	}
 
 	if (path[0] == '/') {
-		*offs = 0;
+		if (offs)
+			*offs = 0;
 		goto out;
 	}
 
@@ -651,7 +656,8 @@ makepathbuf(struct lwp *l, const char *u
 
 	memmove(path, bp, tlen);
 	path[tlen] = '\0';
-	*offs = tlen - len;
+	if (offs)
+		*offs = tlen - len;
 out:
 	*pbp = pathbuf_assimilate(path);
 	return 0;
@@ -730,7 +736,8 @@ execve_loadvm(struct lwp *l, const char 
 	 * functions call check_exec() recursively - for example,
 	 * see exec_script_makecmds().
 	 */
-	if ((error = makepathbuf(l, path, &data->ed_pathbuf, &offs)) != 0)
+	if ((error = exec_makepathbuf(l, path, UIO_USERSPACE,
+	    &data->ed_pathbuf, &offs)) != 0)
 		goto clrflg;
 	data->ed_pathstring = pathbuf_stringcopy_get(data->ed_pathbuf);
 	data->ed_resolvedpathbuf = PNBUF_GET();

Index: src/sys/sys/exec.h
diff -u src/sys/sys/exec.h:1.152 src/sys/sys/exec.h:1.153
--- src/sys/sys/exec.h:1.152	Tue Nov  7 14:44:05 2017
+++ src/sys/sys/exec.h	Fri Apr 27 14:33:24 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec.h,v 1.152 2017/11/07 19:44:05 christos Exp $	*/
+/*	$NetBSD: exec.h,v 1.153 2018/04/27 18:33:24 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -310,7 +310,9 @@ int	check_posix_spawn	(struct lwp *);
 void	posix_spawn_fa_free(struct posix_spawn_file_actions *, size_t);
 int	do_posix_spawn(struct lwp *, pid_t *, bool*, const char *,
     struct posix_spawn_file_actions *, struct posix_spawnattr *,
-    char *const *argv, char *const *, execve_fetch_element_t);
+    char *const *, char *const *, execve_fetch_element_t);
+int      exec_makepathbuf(struct lwp *, const char *, enum uio_seg,
+    struct pathbuf **, size_t *);
 
 extern int	maxexec;
 

Reply via email to