From owner-freebsd-bugs@FreeBSD.ORG Wed Sep 7 12:10:08 2005 Return-Path: X-Original-To: freebsd-bugs@hub.freebsd.org Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9DC7516A41F for ; Wed, 7 Sep 2005 12:10:08 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id B6FF343D48 for ; Wed, 7 Sep 2005 12:10:07 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id j87CA7Zb080990 for ; Wed, 7 Sep 2005 12:10:07 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.3/8.13.1/Submit) id j87CA7ch080989; Wed, 7 Sep 2005 12:10:07 GMT (envelope-from gnats) Resent-Date: Wed, 7 Sep 2005 12:10:07 GMT Resent-Message-Id: <200509071210.j87CA7ch080989@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Andrey Simonenko Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B0CC716A41F for ; Wed, 7 Sep 2005 12:06:45 +0000 (GMT) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from comsys.ntu-kpi.kiev.ua (comsys.ntu-kpi.kiev.ua [195.245.194.142]) by mx1.FreeBSD.org (Postfix) with ESMTP id AF52C43D45 for ; Wed, 7 Sep 2005 12:06:40 +0000 (GMT) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from pm514-9.comsys.ntu-kpi.kiev.ua (pm514-9.comsys.ntu-kpi.kiev.ua [10.18.54.109]) (authenticated bits=0) by comsys.ntu-kpi.kiev.ua (8.12.10/8.12.10) with ESMTP id j87CEP82060587 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 7 Sep 2005 15:14:26 +0300 (EEST) Received: by pm514-9.comsys.ntu-kpi.kiev.ua (Postfix, from userid 1000) id 745642FA; Wed, 7 Sep 2005 15:04:05 +0300 (EEST) Message-Id: <20050907120405.GB295@pm514-9.comsys.ntu-kpi.kiev.ua> Date: Wed, 7 Sep 2005 15:04:05 +0300 From: Andrey Simonenko To: FreeBSD-gnats-submit@FreeBSD.org Cc: Subject: bin/85830: [patch] pam_exec incorrectly works with vfork() X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Sep 2005 12:10:08 -0000 >Number: 85830 >Category: bin >Synopsis: [patch] pam_exec incorrectly works with vfork() >Confidential: no >Severity: serious >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Wed Sep 07 12:10:07 GMT 2005 >Closed-Date: >Last-Modified: >Originator: Andrey Simonenko >Release: FreeBSD 5.4 >Organization: >Environment: >Description: pam_exec PAM module incorrectly works with vfork() system call. It uses childerr variable to report from a vfork'ed child to a parent if execve() in a child failed. There is not guaranty that a parent will see childerr exactly the same as it was set by its child, because compiler can optimize the code. At first time, I decided simply to declare childpid variable as volatile (and this works), but having read some discussions, I moved vfork-execve sequence to separate function vfork_execve() and automatic variable childerr became global volatile variable. >How-To-Repeat: >Fix: diff -ruN pam_exec.orig/pam_exec.c pam_exec/pam_exec.c --- pam_exec.orig/pam_exec.c Tue Sep 6 23:28:39 2005 +++ pam_exec/pam_exec.c Tue Sep 6 23:58:59 2005 @@ -47,11 +47,26 @@ #include #include +static volatile int childerr; + +static pid_t +vfork_execve(const char *prog, char *const argv[], char *const envlist[]) +{ + pid_t pid; + + if ((pid = vfork()) == 0) { + execve(prog, argv, envlist); + childerr = errno; + _exit(1); + } + return (pid); +} + static int _pam_exec(pam_handle_t *pamh __unused, int flags __unused, int argc, const char *argv[]) { - int childerr, status; + int status; char **env, **envlist; pid_t pid; @@ -64,11 +79,7 @@ */ envlist = pam_getenvlist(pamh); childerr = 0; - if ((pid = vfork()) == 0) { - execve(argv[0], argv, envlist); - childerr = errno; - _exit(1); - } + pid = vfork_execve(argv[0], (char *const *)argv, envlist); for (env = envlist; *env != NULL; ++env) free(*env); free(envlist); @@ -81,7 +92,7 @@ return (PAM_SYSTEM_ERR); } if (childerr != 0) { - openpam_log(PAM_LOG_ERROR, "execv(): %m"); + openpam_log(PAM_LOG_ERROR, "execve(): %m"); return (PAM_SYSTEM_ERR); } if (WIFSIGNALED(status)) { >Release-Note: >Audit-Trail: >Unformatted: