Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1387784ybh; Sun, 19 Jul 2020 18:28:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiazy6GVafMcENT/7QHKxBJBpIM7Cb/JVITZ4GZaBLG5oeLM/p5FgqyZRTAncZxqOUn2VC X-Received: by 2002:a05:6402:13d0:: with SMTP id a16mr19283085edx.269.1595208496946; Sun, 19 Jul 2020 18:28:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595208496; cv=none; d=google.com; s=arc-20160816; b=oF0Q47PTs1O7sFeGU2LVKD05hVcRBzoYzDEaFF3WkkerxF3bOQmXhgMoVgCj/Cm6Sb ErYzyUp9wQlut0Gp/il7+MzLIWmgVjAhTFZ/SwCSDIhLDpr5zSMbe4Ou1FaY0CNBs+Iw U4FvQgBqmajM62oKYxa79Wxg8vE+ZLtmoD1Fs4He7eyuqoCxJPnLPSqG7esebH0SFHCF bG7NVLpjuUPtO+0y+Dbj4U61QHnXHxBNasyYV2XXl0PTRfaF8l9IaE2SJnMHxsBTufZZ R1TFczzg6Yns+jVvUdhX+aQ27KGUukqxuRjKlcO1acuZ8eyhoznWXlGi91XYoXn2DIQa izMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=omIvPcttgu5j5wSkx+ZzUh0WkKQ80FhNwSZ+LIcgfdY=; b=hdfBcjiLxwnB/PjzzkTdgdrf3ZlT/rA3OlvDxzI/3GnH8XKQ0SFAwowdzDEriqIvO3 N6d1EpcX4BQUjCGCVAfbcW+iN3fMN7d6ork7DgHlkca5ZWno5FwnHFI8B/27yXCWWWAK wEg6KRbQc3cJNQmq79LSJK3nluFm2C5dxnnDgbxxth5/i9/VZk3NXPex6KL4QhrGPurA ejLlxvn9Pz6s3UJ1lyfBiVORHj1u2uXBIYph2a+CukaKitGYE7A/2SF/QWt4YzdvH+Na RZaRFq3bhfp3YXZEt1jLrqL+wqYJ+JivoXY9LrJMMM0SRwUd8Ox+1PZo9Ukh7cp8lnLm GEjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bFkV4pDN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m26si12360977ejn.667.2020.07.19.18.27.53; Sun, 19 Jul 2020 18:28:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bFkV4pDN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726531AbgGTBZe (ORCPT + 99 others); Sun, 19 Jul 2020 21:25:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726225AbgGTBZd (ORCPT ); Sun, 19 Jul 2020 21:25:33 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC8DBC0619D2 for ; Sun, 19 Jul 2020 18:25:33 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id a9so3712771pjd.3 for ; Sun, 19 Jul 2020 18:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=omIvPcttgu5j5wSkx+ZzUh0WkKQ80FhNwSZ+LIcgfdY=; b=bFkV4pDNWUAjKk14sFZtOi41d9jfYk6ONr1UWvb1qKHVwcKGZapxut84X4PS7DttVk YukMDyq6uwIr53VzLfImMKiGYi4X+b6YuvUzP82E0Xk6FDaGLk4KOmZpbzbOQOQt29ES TEbjQdkE8iHsXTA3pL3oTnXzcpXqCiQnP1ekg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=omIvPcttgu5j5wSkx+ZzUh0WkKQ80FhNwSZ+LIcgfdY=; b=KFCa6KDrd+uipu+Q9e2IUiACUNPCeZYoDUth4ueJDZYMT299o3rFjP3OwwkVSgVkt6 Zc61SBK3/Y1uji30v8b/rVLWgVzBfcghCklfMvIG7HMi6ork2DEuQUQeWOKqy6KVUjFc ljZULAEvVslx/aB4YM7LrA9tRK9YagZk+ELl+p0V5YtYfHyzzOhLtgSPuBYGaOgnaDKz Bwjz3nhiUWBS0Wc29AeVZEAmC/dC4ooWxZHbg7if/8DSo+gXDln+cSI7cHyIKRwfhKX4 a8SA10kHxblScfZSSSfK4gyVmgmYfxGK6Vprr+pDD0FSIpgL3SwOG2qTZardIJ6Ij8+h Es3A== X-Gm-Message-State: AOAM530BCzelB9Ae/7N6wJHwglNZ+BOJt7lHOxkUDqeVWDcVu4EAZ94/ 9MutlIqNA0/Tui1wujGThqoDbHaYmE0= X-Received: by 2002:a17:902:900b:: with SMTP id a11mr2290929plp.315.1595208333314; Sun, 19 Jul 2020 18:25:33 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id g8sm13526791pgr.70.2020.07.19.18.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jul 2020 18:25:32 -0700 (PDT) Date: Sun, 19 Jul 2020 18:25:31 -0700 From: Kees Cook To: Kristen Carlson Accardi Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, arjan@linux.intel.com, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, rick.p.edgecombe@intel.com, Tony Luck Subject: Re: [PATCH v4 09/10] kallsyms: Hide layout Message-ID: <202007191815.D39859C@keescook> References: <20200717170008.5949-1-kristen@linux.intel.com> <20200717170008.5949-10-kristen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200717170008.5949-10-kristen@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 17, 2020 at 10:00:06AM -0700, Kristen Carlson Accardi wrote: > This patch makes /proc/kallsyms display in a random order, rather > than sorted by address in order to hide the newly randomized address > layout. Ah! Much nicer. Is there any reason not to just do this unconditionally, regardless of FGKASLR? It's a smallish dynamic allocation, and displaying kallsyms is hardly fast-path... > > Signed-off-by: Kristen Carlson Accardi > Reviewed-by: Tony Luck > Tested-by: Tony Luck > --- > kernel/kallsyms.c | 163 +++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 162 insertions(+), 1 deletion(-) > > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c > index bb14e64f62a4..45d147f7f10e 100644 > --- a/kernel/kallsyms.c > +++ b/kernel/kallsyms.c > @@ -446,6 +446,12 @@ struct kallsym_iter { > int show_value; > }; > > +struct kallsyms_shuffled_iter { > + struct kallsym_iter iter; > + loff_t total_syms; > + loff_t shuffled_index[]; > +}; > + > int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value, > char *type, char *name) > { > @@ -661,7 +667,7 @@ bool kallsyms_show_value(const struct cred *cred) > } > } > > -static int kallsyms_open(struct inode *inode, struct file *file) > +static int __kallsyms_open(struct inode *inode, struct file *file) > { > /* > * We keep iterator in m->private, since normal case is to > @@ -682,6 +688,161 @@ static int kallsyms_open(struct inode *inode, struct file *file) > return 0; > } > > +/* > + * When function granular kaslr is enabled, we need to print out the symbols > + * at random so we don't reveal the new layout. > + */ > +#if defined(CONFIG_FG_KASLR) > +static int update_random_pos(struct kallsyms_shuffled_iter *s_iter, > + loff_t pos, loff_t *new_pos) > +{ > + loff_t new; > + > + if (pos >= s_iter->total_syms) > + return 0; > + > + new = s_iter->shuffled_index[pos]; > + > + /* > + * normally this would be done as part of update_iter, however, > + * we want to avoid triggering this in the event that new is > + * zero since we don't want to blow away our pos end indicators. > + */ > + if (new == 0) { > + s_iter->iter.name[0] = '\0'; > + s_iter->iter.nameoff = get_symbol_offset(new); > + s_iter->iter.pos = new; > + } > + > + *new_pos = new; > + return 1; > +} > + > +static void *shuffled_start(struct seq_file *m, loff_t *pos) > +{ > + struct kallsyms_shuffled_iter *s_iter = m->private; > + loff_t new_pos; > + > + if (!update_random_pos(s_iter, *pos, &new_pos)) > + return NULL; > + > + return s_start(m, &new_pos); > +} > + > +static void *shuffled_next(struct seq_file *m, void *p, loff_t *pos) > +{ > + struct kallsyms_shuffled_iter *s_iter = m->private; > + loff_t new_pos; > + > + (*pos)++; > + > + if (!update_random_pos(s_iter, *pos, &new_pos)) > + return NULL; > + > + if (!update_iter(m->private, new_pos)) > + return NULL; > + > + return p; > +} > + > +/* > + * shuffle_index_list() > + * Use a Fisher Yates algorithm to shuffle a list of text sections. > + */ > +static void shuffle_index_list(loff_t *indexes, loff_t size) > +{ > + int i; > + unsigned int j; > + loff_t temp; > + > + for (i = size - 1; i > 0; i--) { > + /* pick a random index from 0 to i */ > + get_random_bytes(&j, sizeof(j)); > + j = j % (i + 1); > + > + temp = indexes[i]; > + indexes[i] = indexes[j]; > + indexes[j] = temp; > + } > +} > + > +static const struct seq_operations kallsyms_shuffled_op = { > + .start = shuffled_start, > + .next = shuffled_next, > + .stop = s_stop, > + .show = s_show > +}; > + > +static int kallsyms_random_open(struct inode *inode, struct file *file) > +{ > + loff_t pos; > + struct kallsyms_shuffled_iter *shuffled_iter; > + struct kallsym_iter iter; > + bool show_value; > + > + /* > + * If privileged, go ahead and use the normal algorithm for > + * displaying symbols > + */ > + show_value = kallsyms_show_value(file->f_cred); > + if (show_value) > + return __kallsyms_open(inode, file); > + > + /* > + * we need to figure out how many extra symbols there are > + * to print out past kallsyms_num_syms > + */ > + pos = kallsyms_num_syms; > + reset_iter(&iter, 0); > + do { > + if (!update_iter(&iter, pos)) > + break; > + pos++; > + } while (1); Can this be tracked separately instead of needing to search for it every time? (Looks like it's modules and ftrace? Could they each have a *_num_sysms?) (I need to go read how kallsyms doesn't miscount in general when the symbol table changes out from under it...) -- Kees Cook