Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964779AbeAJCWM (ORCPT + 1 other); Tue, 9 Jan 2018 21:22:12 -0500 Received: from mail-ot0-f193.google.com ([74.125.82.193]:42660 "EHLO mail-ot0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964777AbeAJCWK (ORCPT ); Tue, 9 Jan 2018 21:22:10 -0500 X-Google-Smtp-Source: ACJfBoscI3oVj7yP/05Ls5roEGtSAGa0cuSbNqWY/jICevXdyAf8ZmkQJFpCBNfFHp4QGUiT6viSG+/C98i6T+oE7so= MIME-Version: 1.0 In-Reply-To: <20180110015713.im4atka6sahz7ucx@ast-mbp> References: <151520099201.32271.4677179499894422956.stgit@dwillia2-desk3.amr.corp.intel.com> <151520108080.32271.16420298348259030860.stgit@dwillia2-desk3.amr.corp.intel.com> <87lgh7n2tf.fsf@xmission.com> <20180110015713.im4atka6sahz7ucx@ast-mbp> From: Dan Williams Date: Tue, 9 Jan 2018 18:22:09 -0800 Message-ID: Subject: Re: [PATCH 16/18] net: mpls: prevent bounds-check bypass via speculative execution To: Alexei Starovoitov Cc: Linus Torvalds , "Eric W. Biederman" , Linux Kernel Mailing List , linux-arch@vger.kernel.org, Peter Zijlstra , Netdev , Greg KH , Thomas Gleixner , "David S. Miller" , Elena Reshetova , Alan Cox Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: On Tue, Jan 9, 2018 at 5:57 PM, Alexei Starovoitov wrote: > On Tue, Jan 09, 2018 at 04:48:24PM -0800, Dan Williams wrote: >> >> #define __nospec_array_ptr(base, idx, sz) \ >> ({ \ >> union { typeof(&base[0]) _ptr; unsigned long _bit; } __u; \ >> unsigned long _i = (idx); \ >> unsigned long _s = (sz); \ >> unsigned long _v = (long)(_i | _s - 1 - _i) \ >> >> BITS_PER_LONG - 1; \ >> unsigned long _mask = _v * ~0UL; \ >> OPTIMIZER_HIDE_VAR(_mask); \ >> __u._ptr = &base[_i & _mask]; \ >> __u._bit &= _mask; \ >> __u._ptr; \ >> }) > > _v * ~0UL doesn't seem right and non intuitive. > What's wrong with: > unsigned long _mask = ~(long)(_i | _s - 1 - _i) >> BITS_PER_LONG - 1; Yeah, I noticed it was ok immediately after I sent that. > and why OPTIMIZER_HIDE_VAR ? It was in Linus' original. but that was when it had the ternary conditional, I'll drop it. It does not change the generated assembly. > Could you remove '&' ? Yes, that should be __u.ptr = base + (i & _mask) > since in doesn't work for: > struct { > int fd[4]; > ... > } *fdt; > it cannot be used as array_acces(fdt->fd, ...); > > Could you please drop nospec_ prefix since it is misleading ? When you came up with that tweak you noted: "The following: [..] is generic and no speculative flows." > This macro doesn't prevent speculation. It masks dangerous speculation. At least, I read nospec as "No Spectre" and it is a prefix used in the Spectre-v2 patches. I also want to include the option, with a static branch, to switch it to the hard "no speculation" version with an ifence if worse comes to worse and we find a compiler / cpu where it doesn't work. The default will be the fast and practical implementation. > I think array_access() was the best name so far. For other usages I need the pointer to the array element, also array_access() by itself is unsuitable for __fcheck_files because we still need rcu_dereference_raw() on the element de-reference. So, I think it's better to get a sanitized array element pointer which can be used with rcu, READ_ONCE(), etc... directly rather than try to do the access in the same macro.