Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp562454ybm; Wed, 27 May 2020 02:23:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzuKN+pVpaBF1SeYHwUqjQaO1LreBcAFLEmhBlgs9u5tiRoMrpWYB5lc3BAQBQpM7nBy6j X-Received: by 2002:a50:d55c:: with SMTP id f28mr431418edj.87.1590571404267; Wed, 27 May 2020 02:23:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590571404; cv=none; d=google.com; s=arc-20160816; b=FPVwbNGm19u/CmmAWWfvAp3t1dmwSRsfJe4vcqon737caLYuvtLrQch628Prg3FG0I /or9nPbnZLJA2gLsZK8jhs1pyH4MqzKAHy98M3HI3LvtQMBsgMZEMUcK6Eo1OGSstPKZ BiKPz/MVYhYhQH+925AF79dPAtKeKHECnfTa1r8pcwgkJaBSLTciBZ9f6XzcsEMlSpaM +MfSdflZyREjM2akFurCfcRMBKXwg1bqbVcTvTQB3/qeZuAdIuuY0S8qh3p4Z0R6mMuh EIDpasQTeMtLJ8svjc8xelpDrI6SBhEAxexqU3wFUq2s266CGc/bheL9ihSQpVc7KQyF UDJA== 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; bh=HQol+aX70fIJOmMMw1ea1QUevZIse8SxJINtUk0+4vs=; b=P6SraXdwTocBF11eTszO/S7JEe4mjdq+xn9VtAcRMSPjHP8WUw32bT940M9zSUgrEF XAUNUTWneXO23KvYXQjIFnL1F8J387wbRhkMeLFb/bxKIywM9aGUXjvl3R0PbMsphRpH c5ftQktBUh2zLJyQMf+876Lhr7toCydDCNht7URQiNBjTDrUtjN+oWBk3WWD8kBbcciQ o7ppysB6mhIB2TiC7M7XkyK4TPWyEKRS4QRfD4WMoQKN/6LUeLlj3/pKeu2+jShmJl3S gp6TsiZ1rSvmZwFI3BVbsXUCR996cG59vKkTDQdAmdkI6EOgKmIQ3XsVxKgC7WS8/zgz 0aCw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q15si1211261edi.367.2020.05.27.02.23.00; Wed, 27 May 2020 02:23:24 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392079AbgEZWi1 (ORCPT + 99 others); Tue, 26 May 2020 18:38:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389755AbgEZWi1 (ORCPT ); Tue, 26 May 2020 18:38:27 -0400 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7EC1C061A0F for ; Tue, 26 May 2020 15:38:26 -0700 (PDT) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.93 #3 (Red Hat Linux)) id 1jdiDB-00G1l6-Of; Tue, 26 May 2020 22:38:17 +0000 Date: Tue, 26 May 2020 23:38:17 +0100 From: Al Viro To: Alexander Potapenko Cc: Kees Cook , Andrew Morton , Alexey Dobriyan , LKML , sunhaoyl@outlook.com, x86@kernel.org Subject: Re: [PATCH] fs/binfmt_elf.c: allocate initialized memory in fill_thread_core_info() Message-ID: <20200526223817.GA3819674@ZenIV.linux.org.uk> References: <20200419100848.63472-1-glider@google.com> <20200420153352.6682533e794f591dae7aafbc@linux-foundation.org> <202004201540.01C8F82B@keescook> <20200421034249.GB23230@ZenIV.linux.org.uk> <20200512010901.GQ23230@ZenIV.linux.org.uk> <20200512034400.GA1537486@ZenIV.linux.org.uk> <20200513033349.GR23230@ZenIV.linux.org.uk> <20200524234535.GA23230@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200524234535.GA23230@ZenIV.linux.org.uk> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 25, 2020 at 12:45:35AM +0100, Al Viro wrote: > On Wed, May 13, 2020 at 04:33:49AM +0100, Al Viro wrote: > > > FWIW, what I'm going to do is > > * make all callers of copy_regset_to_user() pass 0 as pos > > (there are very few exceptions - one on arm64, three on sparc32 > > and five on sparc64; I hadn't dealt with arm64 one yet, but all > > cases on sparc are handled) > > [snip] > > Any of that would be easy to backport, though. Several questions > regaring XSAVE and friends: > > * do we ever run on XSAVE/XSAVES-capable hardware with XFEATURE_FP > turned off? > > * is it possible for x86 to have gaps between the state components > area as reported by CPUID 0x0d? IOW, can area for feature 2 > (XFEATURE_YMM) to start *not* at 0x200 and can area for N start > not right after the end of area for N-1 for some N > 2? > > I think I have an easy-to-backport solution, but I'm really confused > about XFEATURE_FP situation... Folks, could you test the following? copy_xstate_to_kernel(): don't leave parts of destination uninitialized copy the corresponding pieces of init_fpstate into the gaps instead. Signed-off-by: Al Viro --- diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 32b153d38748..6a54e83d5589 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -957,18 +957,31 @@ static inline bool xfeatures_mxcsr_quirk(u64 xfeatures) return true; } -/* - * This is similar to user_regset_copyout(), but will not add offset to - * the source data pointer or increment pos, count, kbuf, and ubuf. - */ -static inline void -__copy_xstate_to_kernel(void *kbuf, const void *data, - unsigned int offset, unsigned int size, unsigned int size_total) +static void fill_gap(unsigned to, void **kbuf, unsigned *pos, unsigned *count) { - if (offset < size_total) { - unsigned int copy = min(size, size_total - offset); + if (*pos < to) { + unsigned size = to - *pos; + + if (size > *count) + size = *count; + memcpy(*kbuf, (void *)&init_fpstate.xsave + *pos, size); + *kbuf += size; + *pos += size; + *count -= size; + } +} - memcpy(kbuf + offset, data, copy); +static void copy_part(unsigned offset, unsigned size, void *from, + void **kbuf, unsigned *pos, unsigned *count) +{ + fill_gap(offset, kbuf, pos, count); + if (size > *count) + size = *count; + if (size) { + memcpy(*kbuf, from, size); + *kbuf += size; + *pos += size; + *count -= size; } } @@ -981,8 +994,9 @@ __copy_xstate_to_kernel(void *kbuf, const void *data, */ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset_start, unsigned int size_total) { - unsigned int offset, size; struct xstate_header header; + const unsigned off_mxcsr = offsetof(struct fxregs_state, mxcsr); + unsigned count = size_total; int i; /* @@ -998,46 +1012,42 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of header.xfeatures = xsave->header.xfeatures; header.xfeatures &= ~XFEATURE_MASK_SUPERVISOR; + if (header.xfeatures & XFEATURE_MASK_FP) + copy_part(0, off_mxcsr, + &xsave->i387, &kbuf, &offset_start, &count); + if (header.xfeatures & (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM)) + copy_part(off_mxcsr, MXCSR_AND_FLAGS_SIZE, + &xsave->i387.mxcsr, &kbuf, &offset_start, &count); + if (header.xfeatures & XFEATURE_MASK_FP) + copy_part(offsetof(struct fxregs_state, st_space), 128, + &xsave->i387.st_space, &kbuf, &offset_start, &count); + if (header.xfeatures & XFEATURE_MASK_SSE) + copy_part(xstate_offsets[XFEATURE_MASK_SSE], 256, + &xsave->i387.xmm_space, &kbuf, &offset_start, &count); + /* + * Fill xsave->i387.sw_reserved value for ptrace frame: + */ + copy_part(offsetof(struct fxregs_state, sw_reserved), 48, + xstate_fx_sw_bytes, &kbuf, &offset_start, &count); /* * Copy xregs_state->header: */ - offset = offsetof(struct xregs_state, header); - size = sizeof(header); - - __copy_xstate_to_kernel(kbuf, &header, offset, size, size_total); + copy_part(offsetof(struct xregs_state, header), sizeof(header), + &header, &kbuf, &offset_start, &count); - for (i = 0; i < XFEATURE_MAX; i++) { + for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { /* * Copy only in-use xstates: */ if ((header.xfeatures >> i) & 1) { void *src = __raw_xsave_addr(xsave, i); - offset = xstate_offsets[i]; - size = xstate_sizes[i]; - - /* The next component has to fit fully into the output buffer: */ - if (offset + size > size_total) - break; - - __copy_xstate_to_kernel(kbuf, src, offset, size, size_total); + copy_part(xstate_offsets[i], xstate_sizes[i], + src, &kbuf, &offset_start, &count); } } - - if (xfeatures_mxcsr_quirk(header.xfeatures)) { - offset = offsetof(struct fxregs_state, mxcsr); - size = MXCSR_AND_FLAGS_SIZE; - __copy_xstate_to_kernel(kbuf, &xsave->i387.mxcsr, offset, size, size_total); - } - - /* - * Fill xsave->i387.sw_reserved value for ptrace frame: - */ - offset = offsetof(struct fxregs_state, sw_reserved); - size = sizeof(xstate_fx_sw_bytes); - - __copy_xstate_to_kernel(kbuf, xstate_fx_sw_bytes, offset, size, size_total); + fill_gap(size_total, &kbuf, &offset_start, &count); return 0; }