From owner-svn-src-all@freebsd.org Thu Oct 27 21:23:16 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4D4F9C24686; Thu, 27 Oct 2016 21:23:16 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 07B59987; Thu, 27 Oct 2016 21:23:15 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u9RLNF9i079523; Thu, 27 Oct 2016 21:23:15 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u9RLNENb079520; Thu, 27 Oct 2016 21:23:14 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <201610272123.u9RLNENb079520@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Thu, 27 Oct 2016 21:23:14 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r308004 - in head/sys: amd64/amd64 i386/i386 x86/include X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Oct 2016 21:23:16 -0000 Author: jhb Date: Thu Oct 27 21:23:14 2016 New Revision: 308004 URL: https://svnweb.freebsd.org/changeset/base/308004 Log: MFamd64: Add bounds checks on addresses used with /dev/mem. Reject attempts to read from or memory map offsets in /dev/mem that are beyond the maximum-supported physical address of the current CPU. Reviewed by: kib MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D7408 Modified: head/sys/amd64/amd64/mem.c head/sys/i386/i386/mem.c head/sys/x86/include/x86_var.h Modified: head/sys/amd64/amd64/mem.c ============================================================================== --- head/sys/amd64/amd64/mem.c Thu Oct 27 18:46:52 2016 (r308003) +++ head/sys/amd64/amd64/mem.c Thu Oct 27 21:23:14 2016 (r308004) @@ -140,7 +140,7 @@ memrw(struct cdev *dev, struct uio *uio, error = uiomove((void *)vd, c, uio); break; } - if (v >= (1ULL << cpu_maxphyaddr)) { + if (v > cpu_getmaxphyaddr()) { error = EFAULT; break; } @@ -169,7 +169,7 @@ memmmap(struct cdev *dev, vm_ooffset_t o int prot __unused, vm_memattr_t *memattr __unused) { if (dev2unit(dev) == CDEV_MINOR_MEM) { - if (offset >= (1ULL << cpu_maxphyaddr)) + if (offset > cpu_getmaxphyaddr()) return (-1); *paddr = offset; return (0); Modified: head/sys/i386/i386/mem.c ============================================================================== --- head/sys/i386/i386/mem.c Thu Oct 27 18:46:52 2016 (r308003) +++ head/sys/i386/i386/mem.c Thu Oct 27 21:23:14 2016 (r308004) @@ -108,8 +108,11 @@ memrw(struct cdev *dev, struct uio *uio, continue; } if (dev2unit(dev) == CDEV_MINOR_MEM) { - pa = uio->uio_offset; - pa &= ~PAGE_MASK; + if (uio->uio_offset > cpu_getmaxphyaddr()) { + error = EFAULT; + break; + } + pa = trunc_page(uio->uio_offset); } else { /* * Extract the physical page since the mapping may @@ -162,6 +165,8 @@ memmmap(struct cdev *dev, vm_ooffset_t o int prot __unused, vm_memattr_t *memattr __unused) { if (dev2unit(dev) == CDEV_MINOR_MEM) { + if (offset > cpu_getmaxphyaddr()) + return (-1); *paddr = offset; return (0); } Modified: head/sys/x86/include/x86_var.h ============================================================================== --- head/sys/x86/include/x86_var.h Thu Oct 27 18:46:52 2016 (r308003) +++ head/sys/x86/include/x86_var.h Thu Oct 27 21:23:14 2016 (r308004) @@ -94,6 +94,20 @@ struct trapframe; */ typedef void alias_for_inthand_t(void); +/* + * Returns the maximum physical address that can be used with the + * current system. + */ +static __inline vm_paddr_t +cpu_getmaxphyaddr(void) +{ +#if defined(__i386__) && !defined(PAE) + return (0xffffffff); +#else + return ((1ULL << cpu_maxphyaddr) - 1); +#endif +} + void *alloc_fpusave(int flags); void busdma_swi(void); bool cpu_mwait_usable(void);