From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexei Starovoitov Subject: Re: [PATCH 16/18] net: mpls: prevent bounds-check bypass via speculative execution Date: Tue, 9 Jan 2018 17:57:14 -0800 Message-ID: <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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org To: Dan Williams 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 List-Id: linux-arch.vger.kernel.org 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; and why OPTIMIZER_HIDE_VAR ? Could you remove '&' ? 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 ? This macro doesn't prevent speculation. I think array_access() was the best name so far. From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg0-f67.google.com ([74.125.83.67]:46588 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933256AbeAJB5R (ORCPT ); Tue, 9 Jan 2018 20:57:17 -0500 Date: Tue, 9 Jan 2018 17:57:14 -0800 From: Alexei Starovoitov Subject: Re: [PATCH 16/18] net: mpls: prevent bounds-check bypass via speculative execution Message-ID: <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-arch-owner@vger.kernel.org List-ID: To: Dan Williams 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 Message-ID: <20180110015714.aJ2KcCI64Fqv7AUgTrBudgx-itSJbPGaLzvE2U0iCT8@z> 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; and why OPTIMIZER_HIDE_VAR ? Could you remove '&' ? 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 ? This macro doesn't prevent speculation. I think array_access() was the best name so far.