From owner-freebsd-arch@FreeBSD.ORG Sun Apr 19 19:59:48 2015 Return-Path: Delivered-To: arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 3B010900 for ; Sun, 19 Apr 2015 19:59:48 +0000 (UTC) Received: from mail-ob0-f182.google.com (mail-ob0-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 095DD773 for ; Sun, 19 Apr 2015 19:59:47 +0000 (UTC) Received: by obfe9 with SMTP id e9so103871546obf.1 for ; Sun, 19 Apr 2015 12:59:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=4BjroyI9ShcGr/mhIqVgoERDHW05GuzQ91f8Cc8RFt8=; b=bbd/tYZYn//M/WYn/cy9PS9SOyOoR1/Xw7dcUAf5koaKIL1iW/F6pgPT0A3WHmRItI jNEn8N2cGQi/lLKAdASYk3LvK53e9DzBy1787Wql7bt/Ehfra2aPxjW8wHHCW/ImAE7r Jp5ujOx19kktKK5m5kVxxeyQXFlbA2dQb3HrhHIFVZhSbvd/Mf3btboJpQkMBLflAfGU nFCC/pHTdDDtdorbmV3bspncMD5VP/Oq4coJL6luzeJ6ZfEBKSPOPC1newHGE6wmDKi/ Q0H1VXHoayGQ4Orf2jQobwx12uxHX11/U0xAk2LWaUGb0WAMaiilf44fkB4dmp9nVAf7 xcyw== X-Gm-Message-State: ALoCoQmHADs6up91PJ1i0SeCiQKkE9qgIozCrbpVGjXNOsMqtO5EYrhBYYhLHoAxaLDd6FggPiBn MIME-Version: 1.0 X-Received: by 10.202.84.135 with SMTP id i129mr10730751oib.114.1429473587050; Sun, 19 Apr 2015 12:59:47 -0700 (PDT) Received: by 10.202.80.6 with HTTP; Sun, 19 Apr 2015 12:59:46 -0700 (PDT) In-Reply-To: <20150419085248.GB2390@kib.kiev.ua> References: <20150417075942.GI2390@kib.kiev.ua> <20150419085248.GB2390@kib.kiev.ua> Date: Sun, 19 Apr 2015 21:59:46 +0200 Message-ID: Subject: Re: setproctitle [was: Re: Removal of the 6.x kernel compat code from libc] From: Oliver Pinter To: Konstantin Belousov Cc: "freebsd-arch@freebsd.org" , peter@freebsd.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 19 Apr 2015 19:59:48 -0000 On Sun, Apr 19, 2015 at 10:52 AM, Konstantin Belousov wrote: > On Fri, Apr 17, 2015 at 01:39:04PM +0200, Oliver Pinter wrote: >> Is there any chanche to get ride of the very old (FreeBSD 2.x) compat >> hacks like these: >> https://github.com/freebsd/freebsd/blob/master/lib/libc/gen/setproctitle.c#L40 ? >> > > Below is the promised cleanup of the libc/gen/setproctitle.c. Seems fine to me, and I started a jenkins build with this patch: http://jenkins.hardenedbsd.org:8180/jenkins/job/HardenedBSD-unstable-amd64/changes > > I see a value in keeping the track of the historical interfaces. I > considered moving the explanation about old_ps_strings to sys/exec.h, > where the current ps_strings is defined and explained, but this would > make the safety checks in the setproctitle() code uncomprehensive. Fine. > > diff --git a/lib/libc/gen/setproctitle.c b/lib/libc/gen/setproctitle.c > index cd705fb..9dff328 100644 > --- a/lib/libc/gen/setproctitle.c > +++ b/lib/libc/gen/setproctitle.c > @@ -42,9 +42,10 @@ __FBSDID("$FreeBSD$"); > * 1: old_ps_strings at the very top of the stack. > * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. > * 3: ps_strings at the very top of the stack. > - * This attempts to support a kernel built in the #2 and #3 era. > - */ > - > + * We only support a kernel providing #3 style ps_strings. > + * > + * For historical purposes, a definition of the old ps_strings structure > + * and location is preserved below: > struct old_ps_strings { > char *old_ps_argvstr; > int old_ps_nargvstr; > @@ -53,6 +54,7 @@ struct old_ps_strings { > }; > #define OLD_PS_STRINGS ((struct old_ps_strings *) \ > (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) > + */ > > #include > > @@ -136,41 +138,38 @@ setproctitle(const char *fmt, ...) > ps_strings = (struct ps_strings *)ul_ps_strings; > } > > - /* PS_STRINGS points to zeroed memory on a style #2 kernel */ > - if (ps_strings->ps_argvstr) { > - /* style #3 */ > - if (oargc == -1) { > - /* Record our original args */ > - oargc = ps_strings->ps_nargvstr; > - oargv = ps_strings->ps_argvstr; > - for (i = len = 0; i < oargc; i++) { > - /* > - * The program may have scribbled into its > - * argv array, e.g., to remove some arguments. > - * If that has happened, break out before > - * trying to call strlen on a NULL pointer. > - */ > - if (oargv[i] == NULL) { > - oargc = i; > - break; > - } > - snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", > - len ? " " : "", oargv[i]); > - if (len) > - len++; > - len += strlen(oargv[i]); > - if (len >= SPT_BUFSIZE) > - break; > + /* > + * PS_STRINGS points to zeroed memory on a style #2 kernel. > + * Should not happen. > + */ > + if (ps_strings->ps_argvstr == NULL) > + return; > + > + /* style #3 */ > + if (oargc == -1) { > + /* Record our original args */ > + oargc = ps_strings->ps_nargvstr; > + oargv = ps_strings->ps_argvstr; > + for (i = len = 0; i < oargc; i++) { > + /* > + * The program may have scribbled into its > + * argv array, e.g., to remove some arguments. > + * If that has happened, break out before > + * trying to call strlen on a NULL pointer. > + */ > + if (oargv[i] == NULL) { > + oargc = i; > + break; > } > + snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", > + len != 0 ? " " : "", oargv[i]); > + if (len != 0) > + len++; > + len += strlen(oargv[i]); > + if (len >= SPT_BUFSIZE) > + break; > } > - ps_strings->ps_nargvstr = nargc; > - ps_strings->ps_argvstr = nargvp; > - } else { > - /* style #2 - we can only restore our first arg :-( */ > - if (*obuf == '\0') > - strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr, > - SPT_BUFSIZE - 1); > - OLD_PS_STRINGS->old_ps_nargvstr = 1; > - OLD_PS_STRINGS->old_ps_argvstr = nargvp[0]; > } > + ps_strings->ps_nargvstr = nargc; > + ps_strings->ps_argvstr = nargvp; > }