From nobody Fri Oct 18 08:36:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1719232162; cv=none; d=zohomail.com; s=zohoarc; b=W3Rg6AzVvj6+lDcXipAeQZdyIjFQrqU9+SUKiKc6gmSZDZhNjfKoSAvDI3+2KHqw1ZPhziUyamLtEq6qv6aOCTmDEsCpoJ+ZTz0r0JRcq8AWuwXRnNqs3VKIafusBjlLhuuNHjTAWUl1DftNaJ7RgRbC5Xw7OpzJhOTZ7ZKPvjI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719232162; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=T49lwRJXT9LLlXuByh3ICxUBFnlr1lvpGV1zsmwF7II=; b=e+sMu/Z5Ej4eDZN9azgg585PMMbx73JFFegzMWgPnfL0jZXdHHB7JCFwo34VKfFc13NyCDGNIlTP1dbnSeLRKYRQbaAFVx8YmPSV0wc6fP5SuC5G+aJj+k8D/gennMZe5dhg2dBoPGeEFB1Z29jhPo3BufI0gJWVYinDylmDInI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1719232162099669.9929206619743; Mon, 24 Jun 2024 05:29:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.746548.1153633 (Exim 4.92) (envelope-from ) id 1sLiol-0007to-FY; Mon, 24 Jun 2024 12:29:07 +0000 Received: by outflank-mailman (output) from mailman id 746548.1153633; Mon, 24 Jun 2024 12:29:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sLiol-0007th-Bw; Mon, 24 Jun 2024 12:29:07 +0000 Received: by outflank-mailman (input) for mailman id 746548; Mon, 24 Jun 2024 12:29:06 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sLiok-0007tZ-DZ for xen-devel@lists.xenproject.org; Mon, 24 Jun 2024 12:29:06 +0000 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [2a00:1450:4864:20::232]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 58002706-3225-11ef-b4bb-af5377834399; Mon, 24 Jun 2024 14:29:04 +0200 (CEST) Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2ec0f3b9cfeso49606561fa.0 for ; Mon, 24 Jun 2024 05:29:04 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fa2921e21dsm28243335ad.94.2024.06.24.05.28.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jun 2024 05:29:03 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 58002706-3225-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1719232144; x=1719836944; darn=lists.xenproject.org; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=T49lwRJXT9LLlXuByh3ICxUBFnlr1lvpGV1zsmwF7II=; b=Bm3HaTUYp+Bvpo24AE1Ah8hl2LpEU8tn0swwQYCvLIp0B7J68t/GtDHbvvV2wwuB8J xlsesLTgVJjXUEV5qMiekaE4+/OymUYyO1Q6Uggt10oQ6UNRUWcqG6kUs/sPIP9ChVvy LDjeKr7q6M2h6sPlVlLhr6pOs0oNhrfCUvr6hJpIO0drxxxoBd+upNN2W9XgTFp/rUk7 Q8OtOCuZ0Nl2Eq7vbyfwuCZAQ5wnN+8tSBurECM2CPWu4J1zhV51WFrJa+QsUYiTuD8m qEfndjhTm0DsZosy8/TZo02RjhEbRf6ghPyV9eVPqrl7+UgPsO0M+axR78h9Hw5OgdMv jPMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719232144; x=1719836944; h=content-transfer-encoding:autocrypt:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T49lwRJXT9LLlXuByh3ICxUBFnlr1lvpGV1zsmwF7II=; b=sTI7Xn07xsjS584MgkRpRW/VHzmuu4+Xn9abSM7OPn8LKFmeuFLGwwmD+sARf1fFiF vCps/McVnr1Y1Dz+W9GJUD1g6O3XuEZCc8GQei1l4jfc19ZYuJbbr8L+JDngJ2rrWej7 NKTsePnP4LWUN28i/ysHAMVB6/xmuqor0T5OkKtEyVTfWysd6ulUx2cnIKinQPrQARbZ uT3pIfyOlE74OOAjN2JlWFwvL9Qa/e8iQ9Y303RlWJqb0sb7GCQwItV6PvWeH9MWPFd3 gvN3lVGTmupD4HEHnwHdt9fnr3JhBKUA25SOkBJea7pgLBnzP5WtvcCawe/tRecAk+M+ zDxQ== X-Gm-Message-State: AOJu0Yw1fnwtspyaiC00LrH25hjAmElnCxPBg4zYt2KRucAaQwwl3B1h ydhdhbKQ4BxCiZHYdB2ADBfLHkrd3UatvZpC312lUwvc4pm2ATaN6GHqtfx+3FM5FGEFxSBSh84 = X-Google-Smtp-Source: AGHT+IFQy9PeRggWTF/W705bvZkk2NMYDXLGeQ+j+VInT5+eS6MDMGHKHpKBJsqONdI8wByiG7+GDw== X-Received: by 2002:a2e:98d6:0:b0:2ec:4d74:8025 with SMTP id 38308e7fff4ca-2ec593d93abmr28361281fa.20.1719232143755; Mon, 24 Jun 2024 05:29:03 -0700 (PDT) Message-ID: Date: Mon, 24 Jun 2024 14:28:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , George Dunlap , Julien Grall , Stefano Stabellini , Oleksii Kurochko From: Jan Beulich Subject: [PATCH for-4.19?] xen: avoid UB in guest handle field accessors Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS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AuWpQkjM1ASeQwSHEeAWPgskBQL Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1719232162748100001 Content-Type: text/plain; charset="utf-8" Much like noted in 43d5c5d5f70b ("xen: avoid UB in guest handle arithmetic"), address calculations involved in accessing a struct field can overflow, too. Cast respective pointers to "unsigned long" and convert type checking accordingly. Remaining arithmetic is, despite there possibly being mathematical overflow, okay as per the C99 spec: "A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type." The overflow that we need to guard against is checked for in array_access_ok(). While there add the missing (see {,__}copy_to_guest_offset()) is-not- const checks to {,__}copy_field_to_guest(). Typically, but not always, no change to generated code; code generation (register allocation) is different for at least common/grant_table.c. Signed-off-by: Jan Beulich Acked-by: Andrew Cooper --- I notice that {,__}copy_field_from_guest() are actually unused, which may not be all that surprising: There's perhaps little point in copying in just a single field, as then any further input fields of a struct would likely also need copying that way (to avoid multi-read issues). copy_field_to_guest() has a mere two callers, in x86. All other sites use __copy_field_to_guest(). Overall there may hence be room here for simplification / reduction of redundancy. --- unstable.orig/xen/include/xen/guest_access.h 2024-06-24 13:48:30.384279= 937 +0200 +++ unstable/xen/include/xen/guest_access.h 2024-06-24 13:51:35.390248096 += 0200 @@ -95,16 +95,23 @@ /* Copy sub-field of a structure to guest context via a guest handle. */ #define copy_field_to_guest(hnd, ptr, field) ({ \ const typeof(&(ptr)->field) _s =3D &(ptr)->field; \ - void *_d =3D &(hnd).p->field; \ - (void)(&(hnd).p->field =3D=3D _s); \ - raw_copy_to_guest(_d, _s, sizeof(*_s)); \ + unsigned long d_ =3D (unsigned long)(hnd).p; \ + /* Check that the handle is not for a const type */ \ + void *__maybe_unused _t =3D (hnd).p; \ + (void)((typeof_field(typeof(*(hnd).p), field) *)NULL =3D=3D _s); \ + raw_copy_to_guest((void *)(d_ + offsetof(typeof(*(hnd).p), field)), \ + _s, sizeof(*_s)); \ }) =20 /* Copy sub-field of a structure from guest context via a guest handle. */ #define copy_field_from_guest(ptr, hnd, field) ({ \ - const typeof(&(ptr)->field) _s =3D &(hnd).p->field; \ + unsigned long s_ =3D (unsigned long)(hnd).p; \ typeof(&(ptr)->field) _d =3D &(ptr)->field; \ - raw_copy_from_guest(_d, _s, sizeof(*_d)); \ + (void)((typeof_field(typeof(*(hnd).p), field) *)NULL =3D=3D _d); \ + raw_copy_from_guest(_d, \ + (const void *)(s_ + \ + offsetof(typeof(*(hnd).p), field)), \ + sizeof(*_d)); \ }) =20 #define copy_to_guest(hnd, ptr, nr) \ @@ -149,15 +156,22 @@ =20 #define __copy_field_to_guest(hnd, ptr, field) ({ \ const typeof(&(ptr)->field) _s =3D &(ptr)->field; \ - void *_d =3D &(hnd).p->field; \ - (void)(&(hnd).p->field =3D=3D _s); \ - __raw_copy_to_guest(_d, _s, sizeof(*_s)); \ + unsigned long d_ =3D (unsigned long)(hnd).p; \ + /* Check that the handle is not for a const type */ \ + void *__maybe_unused _t =3D (hnd).p; \ + (void)((typeof_field(typeof(*(hnd).p), field) *)NULL =3D=3D _s); \ + __raw_copy_to_guest((void *)(d_ + offsetof(typeof(*(hnd).p), field)), \ + _s, sizeof(*_s)); \ }) =20 #define __copy_field_from_guest(ptr, hnd, field) ({ \ - const typeof(&(ptr)->field) _s =3D &(hnd).p->field; \ + unsigned long s_ =3D (unsigned long)(hnd).p; \ typeof(&(ptr)->field) _d =3D &(ptr)->field; \ - __raw_copy_from_guest(_d, _s, sizeof(*_d)); \ + (void)((typeof_field(typeof(*(hnd).p), field) *)NULL =3D=3D _d); \ + __raw_copy_from_guest(_d, \ + (const void *)(s_ + \ + offsetof(typeof(*(hnd).p), field)), \ + sizeof(*_d)); \ }) =20 #define __copy_to_guest(hnd, ptr, nr) \