From owner-freebsd-hackers@FreeBSD.ORG Tue Nov 1 07:07:20 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 242DA106566B; Tue, 1 Nov 2011 07:07:20 +0000 (UTC) (envelope-from to.my.trociny@gmail.com) Received: from mail-fx0-f54.google.com (mail-fx0-f54.google.com [209.85.161.54]) by mx1.freebsd.org (Postfix) with ESMTP id 7E7298FC13; Tue, 1 Nov 2011 07:07:19 +0000 (UTC) Received: by faar19 with SMTP id r19so8914218faa.13 for ; Tue, 01 Nov 2011 00:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:organization:references:sender:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=KcLrWKLigJNtPhFhJFQIoKzyGnbi/Aajn66+NJ8R60E=; b=E+uPUzv6aN3yabpB+uY4fxFrjasGM/3HuOD7yJ6K0C02sw7SD+l1wFS92d+42BvNsT 2kGRj5t5uT3CuQHg4cZyHqMfHYaEvV1/+IwLLlCwr6+y7fDS5GKWJcVmtMN8dpqTneF0 1VxLODhB9E8EqxyWHQqycSQkfOhbclPmg5YzQ= Received: by 10.223.4.215 with SMTP id 23mr35033212fas.8.1320131238370; Tue, 01 Nov 2011 00:07:18 -0700 (PDT) Received: from localhost ([94.27.39.186]) by mx.google.com with ESMTPS id y17sm42925297faa.22.2011.11.01.00.07.13 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 Nov 2011 00:07:13 -0700 (PDT) From: Mikolaj Golub To: Kostik Belousov Organization: TOA Ukraine References: <86y5wkeuw9.fsf@kopusha.home.net> <20111016171005.GB50300@deviant.kiev.zoral.com.ua> <86aa8qozyx.fsf@kopusha.home.net> <20111025082451.GO50300@deviant.kiev.zoral.com.ua> <86aa8k2im0.fsf@kopusha.home.net> <20111031094948.GB50300@deviant.kiev.zoral.com.ua> Sender: Mikolaj Golub Date: Tue, 01 Nov 2011 09:07:11 +0200 In-Reply-To: <20111031094948.GB50300@deviant.kiev.zoral.com.ua> (Kostik Belousov's message of "Mon, 31 Oct 2011 11:49:48 +0200") Message-ID: <86boswjp7k.fsf@in138.ua3> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: freebsd-hackers@freebsd.org, Robert Watson Subject: Re: "ps -e" without procfs(5) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Nov 2011 07:07:20 -0000 On Mon, 31 Oct 2011 11:49:48 +0200 Kostik Belousov wrote: KB> For PROC_ARG and PROC_ENV, you blindly trust the read values of the arg and KB> env vector sizes. This can easily cause kernel panics due to unability to KB> malloc the requested memory. I recommend to put some clump, and twice KB> of (PATH_MAX + ARG_MAX) is probably enough (see kern_exec.c, in particular, KB> exec_alloc_args). Also, you might use the swappable memory for the strings KB> as well, in the style of exec_alloc_args(). After looking at it more closely, I am not sure if I need to use exec_alloc_args. I malloc explicitly only for array vector (proc_vector). And actually it should be much smaller than 2 * (PATH_MAX + ARG_MAX). Currently in linprocfs the limit is 512 entries: #define MAX_ARGV_STR 512 /* Max number of argv-like strings */ The same limit is in libkvm: /* * Check that there aren't an unreasonable number of arguments, * and that the address is in user space. Special test for * VM_MIN_ADDRESS as it evaluates to zero, but is not a simple zero * constant for some archs. We cannot use the pre-processor here and * for some archs the compiler would trigger a signedness warning. */ if (narg > 512 || addr + 1 < VM_MIN_ADDRESS + 1 || addr >= VM_MAXUSER_ADDRESS) return (0); (BTW, may be the VM_MIN_ADDRESS - VM_MAXUSER_ADDRESS is worth adding in my code too?) So it looks like I should use the same limit (512 * sizeof(char *)) for the allocated array. I could use exec_alloc_args() for the allocation but it would reqire some changes: I would have to free using kmem_free_wakeup(), which requires size of the region, while I return the number of entries. So I'd rather not use exec_alloc_args() for vector allocation because the benefit is not significant here. For strings I use sbuf and set it up using sbuf_new_for_sysctl. I could set it up manually as SBUF_FIXEDLEN allocating buf (up to 2 * (PATH_MAX + ARG_MAX)) with exec_alloc_args() but this would complicate things a little. Do you think it is worth doing? -- Mikolaj Golub