From owner-freebsd-current@FreeBSD.ORG Mon Mar 25 13:34:30 2013 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 1E119B7A for ; Mon, 25 Mar 2013 13:34:30 +0000 (UTC) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from comsys.kpi.ua (comsys.kpi.ua [77.47.192.42]) by mx1.freebsd.org (Postfix) with ESMTP id CE9B6E02 for ; Mon, 25 Mar 2013 13:34:29 +0000 (UTC) Received: from pm513-1.comsys.kpi.ua ([10.18.52.101] helo=pm513-1.comsys.ntu-kpi.kiev.ua) by comsys.kpi.ua with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1UK7Xd-0001sP-Ot; Mon, 25 Mar 2013 15:34:25 +0200 Received: by pm513-1.comsys.ntu-kpi.kiev.ua (Postfix, from userid 1001) id CC8B81E08A; Mon, 25 Mar 2013 15:34:24 +0200 (EET) Date: Mon, 25 Mar 2013 15:34:24 +0200 From: Andrey Simonenko To: Rick Macklem Subject: Re: Possible bug in NFSv4 with krb5p security? Message-ID: <20130325133424.GA1342@pm513-1.comsys.ntu-kpi.kiev.ua> References: <20130220122826.GA13613@pm513-1.comsys.ntu-kpi.kiev.ua> <423251068.3167476.1361402947119.JavaMail.root@erie.cs.uoguelph.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <423251068.3167476.1361402947119.JavaMail.root@erie.cs.uoguelph.ca> User-Agent: Mutt/1.5.21 (2010-09-15) X-Authenticated-User: simon@comsys.ntu-kpi.kiev.ua X-Authenticator: plain X-Sender-Verify: SUCCEEDED (sender exists & accepts mail) X-Exim-Version: 4.63 (build at 28-Apr-2011 07:11:12) X-Date: 2013-03-25 15:34:25 X-Connected-IP: 10.18.52.101:51062 X-Message-Linecount: 92 X-Body-Linecount: 75 X-Message-Size: 3462 X-Body-Size: 2634 Cc: freebsd-current@freebsd.org, Elias Martenson , Benjamin Kaduk X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Mar 2013 13:34:30 -0000 On Wed, Feb 20, 2013 at 06:29:07PM -0500, Rick Macklem wrote: > Andrey Simonnenko wrote: > > > > Another variant. This is a program that can be used for verifying > > correctness of the function, just change PWBUF_SIZE_* values and put > > some printfs to see when buffer is reallocated. sizehint is updated > > only when malloc() succeeded. > > > > --------------------------------- > > static int > > getpwnam_r_func(const char *name, uid_t *uidp) > > { > > #define PWBUF_SIZE_INI (2 * MAXLOGNAME + MAXPATHLEN + _PASSWORD_LEN) > > #define PWBUF_SIZE_INC 128 > > > > static size_t sizehint = PWBUF_SIZE_INI; > > > > struct passwd pwd; > > struct passwd *pw; > > char *buf; > > size_t size; > > int error; > > char lname[MAXLOGNAME]; > > char bufs[PWBUF_SIZE_INI]; > > > > strncpy(lname, name, sizeof(lname)); > > > > buf = bufs; > > size = sizeof(bufs); > > for (;;) { > > error = getpwnam_r(lname, &pwd, buf, size, &pw); > > if (buf != bufs) > > free(buf); > > if (pw != NULL) { > > *uidp = pw->pw_uid; > > return (GSS_S_COMPLETE); > > } else if (error != ERANGE || size > SIZE_MAX - PWBUF_SIZE_INC) > > return (GSS_S_FAILURE); > > if (size != sizehint) > > size = sizehint; > > else > > size += PWBUF_SIZE_INC; > > buf = malloc(size); > > if (buf == NULL) > > return (GSS_S_FAILURE); > > sizehint = size; > > } > > } > > > All looks fine to me. (Before my mailer messed with the whitespace;-) > > Thanks, rick > ps: I will commit it in April, unless someone else does so sooner. I was thinking about this approach once again and made a conclusion that it is wrong. Using static buffer at first and then allocate memory for next calls can cause slowdown, depends on number of entries and used database backend of course. The libc code for getpwnam() allocates memory for the buffer and does not free it on exit from the function, If the above written code or any of its modification is going to be committed to the source base (by you or by some another committer), then I ask and require to not write/mention my name and email address neither in source file nor in commit log message. Appropriate commit log message for the above written code can be the following: -------------------------------------------------------------------------- Since FreeBSD does not support sysconf(_SC_GETPW_R_SIZE_MAX), then allocate a buffer of sufficient size for getpwnam_r() as it is suggested in EXAMPLES of SUSv4 documentation for the getpwnam_r() function. -------------------------------------------------------------------------- since this documentation has similar code.