Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp5321390imm; Fri, 18 May 2018 23:02:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqy0Sj6rHtZCSCwrU81l68KKnWKgS7wgKek6uH3mkjGr2bcobVy6FEV+mp/XneV5xHltlFf X-Received: by 2002:a62:f713:: with SMTP id h19-v6mr11970130pfi.165.1526709768224; Fri, 18 May 2018 23:02:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526709768; cv=none; d=google.com; s=arc-20160816; b=KGHmovIY5A7Gu+74nlyRWWcy576hzrFH46WgIPem/Q7f+RMzb2NmCCqXHL/hjQpkOo KpoprpchCgqocBJXTeFs5021QI+YLukCagIQuc/ZWmKzRGmU8bsTT4Yt1+DMPs5n8/NU 135YeJ3seI4M5mN01e6vxWxxNjG/cXGtELlBkKtHP1F2t3ZLTmO6oQcQGn1fpUPUSaad tEqcgkYlTnL6P2Gpl1GE3e9nhM88DScrRRk9ta7QDlf6rdBG7YPTHx/n7dxy6P0r7jER pQQqh/nxAs/STIK/R5ctLeBwfcXm2+B1HfB0Y0ZWEgvmZ5RjOiEM4f7MjnR+tcWJWexH rNoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=nl+XkFnJ9/9bH9l7y3em3usnz6kXwbBAhyGwUTrVJyI=; b=0K0mE6AVDNqbzpr6b/nLh4dZq/+9yojryI5nSHwZsLCtMPITIniF4Kesvk54WPw9OF M7m1wShojFiXJkf6JuuMxic14rwvBcRxaX2lk8xqk1veA0rjSivR69/MRZUDvFQuVZOr DwCilqgZcEPrVcgX85uPYjPLqhnfZ1ewC7ogWRYhBbrQ2bf1L40VOToP3FaHyIjDIE8x UCcfft/KC52vdbLXMs58iNebRGATl54l2NOqdFEWipVIAP/OoxmgconTFea2K2eAvdbV svkmmQtc4ROA6RjIkaAYFhhEZr4EmJmRzV0VeVi7Pu4TN1mwqhEhrhku13hBex7KJRag o4hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=vzWJqtkx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f2-v6si9949047pli.569.2018.05.18.23.02.32; Fri, 18 May 2018 23:02:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=vzWJqtkx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750864AbeESGCW (ORCPT + 99 others); Sat, 19 May 2018 02:02:22 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:55112 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750743AbeESGCT (ORCPT ); Sat, 19 May 2018 02:02:19 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w4J5m7v7161664; Sat, 19 May 2018 06:01:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2017-10-26; bh=nl+XkFnJ9/9bH9l7y3em3usnz6kXwbBAhyGwUTrVJyI=; b=vzWJqtkxAs8lue/VCyaAOP0wdq/+hdS13CuKlccgAeFWzd6Iu6x62HXbmMw1Vg7E11C6 h9Ep9kOSad9E34Pto5x581I83CqyRw3yvtKSgpATyZlIYmH3U5SXFn/RGOCZLsNscGe0 E53C9WqECWPLqKZOmjYx+K7QJhjUYG+K7zDsTt0AcHZFFKs4NfTboTVWg3zmiacFQd8T gfsL7GY83Gf2KUIqP/dVSkV/ZtzoqOIlZ0MKI4Q1ycsP/3+Z/H/6QG6FpRU4cnogYIs6 bA26K5ynhe6NLd8tVVJ1r5/xLCHl4MJMpvuWrVX6lmUC/vEeCyZ44TnAZTmesEwxItDB SA== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2120.oracle.com with ESMTP id 2j2ck982mu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 19 May 2018 06:01:51 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w4J61o5v002201 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 19 May 2018 06:01:50 GMT Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w4J61n88015210; Sat, 19 May 2018 06:01:49 GMT Received: from mwanda (/197.254.35.146) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 18 May 2018 23:01:48 -0700 Date: Sat, 19 May 2018 09:01:36 +0300 From: Dan Carpenter To: Joerg Roedel Cc: Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: [PATCH] KVM: X86: prevent integer overflows in KVM_MEMORY_ENCRYPT_REG_REGION Message-ID: <20180519060135.GA4991@mwanda> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Mailer: git-send-email haha only kidding User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8897 signatures=668699 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1805190050 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a fix from reviewing the code, but it looks like it might be able to lead to an Oops. It affects 32bit systems. The KVM_MEMORY_ENCRYPT_REG_REGION ioctl uses a u64 for range->addr and range->size but the high 32 bits would be truncated away on a 32 bit system. This is harmless but it's also harmless to prevent it. Then in sev_pin_memory() the "uaddr + ulen" calculation can wrap around. The wrap around can happen on 32 bit or 64 bit systems, but I was only able to figure out a problem for 32 bit systems. We would pick a number which results in "npages" being zero. The sev_pin_memory() would then return ZERO_SIZE_PTR without allocating anything. I made it illegal to call sev_pin_memory() with "ulen" set to zero. Hopefully, that doesn't cause any problems. I also changed the type of "first" and "last" to long, just for cosmetic reasons. Otherwise on a 64 bit system you're saving "uaddr >> 12" in an int and it truncates the high 20 bits away. The math works in the current code so far as I can see but it's just weird. Signed-off-by: Dan Carpenter --- Again, this is a static checker fix. The most risky parts of this patch are blocking "ulen == 0" and changing the types of "first" and "last". I felt like those changes made the math easier to understand diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 220e5a89465a..de21d5c5168b 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -1762,7 +1762,10 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, unsigned long npages, npinned, size; unsigned long locked, lock_limit; struct page **pages; - int first, last; + unsigned long first, last; + + if (ulen == 0 || uaddr + ulen < uaddr) + return NULL; /* Calculate number of pages. */ first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; @@ -6925,6 +6928,9 @@ static int svm_register_enc_region(struct kvm *kvm, if (!sev_guest(kvm)) return -ENOTTY; + if (range->addr > ULONG_MAX || range->size > ULONG_MAX) + return -EINVAL; + region = kzalloc(sizeof(*region), GFP_KERNEL); if (!region) return -ENOMEM;