Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5044879imu; Wed, 19 Dec 2018 04:48:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/UKAq1j4rkfwFfU21s/JlUhyXQ2QiHxBvVSy7iT6U//eRq4zPx2k4W6PT6sWaiurwps68zG X-Received: by 2002:a62:1484:: with SMTP id 126mr5902158pfu.257.1545223739426; Wed, 19 Dec 2018 04:48:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545223739; cv=none; d=google.com; s=arc-20160816; b=okOfs71HbCj0Lzr/wlCf5fy5jIm5oHKceGagnqgE/B4p7GFwjn1YIbX3ZyWUWPcWBf lIuc2MefKJWy00eb7CVBzwnQLDCnl3MKiRjf0cKLd1lteBLInvhQydD+lYiUmTKo/heQ plFBUazxoO+lz151MJRojSOsXzzDa67eB579vFrAJBeRh9woGhXzRN5r4eMQ9HMs/tOi uHiHq911THURMcDGwoeNtpOS80rvRlmTPIqoRMjvRqC1VRzxYb+Aqs8Eu6OuNZ2oTsWL dWGNDS2M9CAW50HZVLe9D30ctAfbziTJ7LAQcyrkcvOdHUL+4rK5RvIdhzUJFlRtghYO jNLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=MmCbkEW++YuRmQ+98HC/KWmVUv+08Nqy0oV8otnoXgY=; b=aW/CwvAPdvyN+5S45EeV9tsaHX6YwG0YLk8AK5KzCBV0TSmASsRRCAQsdG83eQaU37 6GAP/VY3bRPEaN0RrrJfZLMo/hpAVo7AA7TdTIitwrFjTf9BEHfVsCJ0mFooqzS/b3dK f34go1dpyMRYjYEq+4f/BClRQsc91+zlXCKsNsfXRdMwp9tjiMq1seL2rz/+h4G00JWY KXGKtvZH70fXNAQ52rTClLFUm7MBSJ2eADcfnujZVFSQm2hvqfmHkvEJ339i4CDEyeRl OWfSt8Wm2uw0y+LRMOlsaJ9vLGc+yAuwSYo7PaqvOdjW0gWwCsGUCycF2/I9PtzUbeUq oKmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PMD6pcGP; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 30si15970364pgr.396.2018.12.19.04.48.43; Wed, 19 Dec 2018 04:48:59 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=PMD6pcGP; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728737AbeLSLqZ (ORCPT + 99 others); Wed, 19 Dec 2018 06:46:25 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:37762 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728687AbeLSLqZ (ORCPT ); Wed, 19 Dec 2018 06:46:25 -0500 Received: by mail-lf1-f68.google.com with SMTP id y11so14810694lfj.4 for ; Wed, 19 Dec 2018 03:46:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=MmCbkEW++YuRmQ+98HC/KWmVUv+08Nqy0oV8otnoXgY=; b=PMD6pcGPvYQfVlimSaQMVPyVS9d+bmeK2YP82iGQ6Q9WEC/vIZUlsdB7Zb5aO+youk hPWS/DlKkzVX4wy2WWPon5YsyTgt/SgdBOkmw7yJUbvDihm/AaGuxwH3ZwQuCvGdTdsa x1vVWzQRrQTj5uJonK8WHSz3lO/mogdkA2SjPHvFQQPjdgt55QdogVXp85JddezTvb7T M7OIkwdOUVO0LQhwng/AiRvMZ4F5RcbbuuRMc2FZic6HkzaVrpVaJCjOoQ9nctatB7Sa gK/ZeWqB25pTXvn/9P2rkwkeHnTpemgiz0fR1akMY/aE+kUlql7ui8WRUIxFgF/6Md2B JTfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=MmCbkEW++YuRmQ+98HC/KWmVUv+08Nqy0oV8otnoXgY=; b=W45uem+qTlLsitB+LF+3KNVpdwRXCg/AAJ2v38psQUpAO5mhQuzgBPLNfxF5/+US+f 7xSgjcMmshaK1SmeVinOD3F5Az60deWc3YH4cSJjlc1mq85RmBbtmBjbLPNmT3T97/bt v1mhRbvsQPAlGY7yYG+GhryloForeLtzDsgtJtQABqWBT66WACtqgA1m4/y248LB2GOL YkOcX718W1XUSAxPRMUwjUquELgjXLhck89sciLtPPU/xt4Qmkhyt7Xb5uEMig0biqNS 0N+M40//3eQNrDMUTMjAHb3iEXZHuwdTyGMhdJGoBu/43u1RfsLfkLKWRP2lM0rzROKF kGHA== X-Gm-Message-State: AA+aEWY6EH8gBAVMgE5bpVdIkHRBYjYEDeOsv0CwHtZFlFnsUWaO+JMA kbcyfpk1RkbYbFhCvPRnow48s9G5FHRygQqCWWo= X-Received: by 2002:a19:2906:: with SMTP id p6mr4141635lfp.17.1545219982281; Wed, 19 Dec 2018 03:46:22 -0800 (PST) MIME-Version: 1.0 References: <20181217202334.GA11758@jordon-HP-15-Notebook-PC> <20181218095709.GJ26090@n2100.armlinux.org.uk> <20181218123318.GN26090@n2100.armlinux.org.uk> <20181218130146.GO26090@n2100.armlinux.org.uk> <20181219093230.GS26090@n2100.armlinux.org.uk> In-Reply-To: <20181219093230.GS26090@n2100.armlinux.org.uk> From: Souptick Joarder Date: Wed, 19 Dec 2018 17:16:09 +0530 Message-ID: Subject: Re: [PATCH v4 4/9] drm/rockchip/rockchip_drm_gem.c: Convert to use vm_insert_range To: Russell King - ARM Linux Cc: Michal Hocko , Heiko Stuebner , Linux-MM , airlied@linux.ie, hjc@rock-chips.com, Matthew Wilcox , linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, dri-devel@lists.freedesktop.org, Andrew Morton , linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 19, 2018 at 3:02 PM Russell King - ARM Linux wrote: > > On Wed, Dec 19, 2018 at 09:01:09AM +0530, Souptick Joarder wrote: > > On Tue, Dec 18, 2018 at 6:31 PM Russell King - ARM Linux > > wrote: > > > > > > On Tue, Dec 18, 2018 at 06:24:29PM +0530, Souptick Joarder wrote: > > > > On Tue, Dec 18, 2018 at 6:03 PM Russell King - ARM Linux > > > > wrote: > > > > > > > > > > On Tue, Dec 18, 2018 at 05:36:04PM +0530, Souptick Joarder wrote: > > > > > > On Tue, Dec 18, 2018 at 3:27 PM Russell King - ARM Linux > > > > > > wrote: > > > > > > > This looks like a change in behaviour. > > > > > > > > > > > > > > If user_count is zero, and offset is zero, then we pass into > > > > > > > vm_insert_range() a page_count of zero, and vm_insert_range() does > > > > > > > nothing and returns zero. > > > > > > > > > > > > > > However, as we can see from the above code, the original behaviour > > > > > > > was to return -ENXIO in that case. > > > > > > > > > > > > I think these checks are not necessary. I am not sure if we get into mmap > > > > > > handlers of driver with user_count = 0. > > > > > > > > > > I'm not sure either, I'm just pointing out the change of behaviour. > > > > > > > > Ok. I think feedback from Heiko might be helpful here :) > > > > > > > > > > > > > > > > The other thing that I'm wondering is that if (eg) count is 8 (the > > > > > > > object is 8 pages), offset is 2, and the user requests mapping 6 > > > > > > > pages (user_count = 6), then we call vm_insert_range() with a > > > > > > > pages of rk_obj->pages + 2, and a pages_count of 6 - 2 = 4. So we > > > > > > > end up inserting four pages. > > > > > > > > > > > > Considering the scenario, user_count will remain 8 (user_count = > > > > > > vma_pages(vma) ). ? No ? > > > > > > Then we call vm_insert_range() with a pages of rk_obj->pages + 2, and > > > > > > a pages_count > > > > > > of 8 - 2 = 6. So we end up inserting 6 pages. > > > > > > > > > > > > Please correct me if I am wrong. > > > > > > > > > > vma_pages(vma) is the number of pages that the user requested, it is > > > > > the difference between vma->vm_end and vma->vm_start in pages. As I > > > > > said above, "the user requests mapping 6 pages", so vma_pages() will > > > > > be 6, and so user_count will also be 6. You are passing > > > > > user_count - offset into vm_insert_range(), which will be 6 - 2 = 4 > > > > > in my example. This is two pages short of what the user requested. > > > > > > > > > > > > > So, this should be the correct behavior. > > > > > > > > return vm_insert_range(vma, vma->vm_start, > > > > rk_obj->pages + offset, > > > > user_count); > > > > > > ... and by doing so, you're introducing another instance of the same > > > bug I pointed out in patch 2. > > > > Sorry but didn't get it ? How it will be similar to the bug pointed > > out in patch 2 ? > Thanks for the detail explanation. > Towards the top of this function, you have: > > unsigned long user_count = vma_pages(vma); > > So what you are proposing does: > > return vm_insert_range(vma, vma->vm_start, rk_obj->pages + offset, > vma_pages(vma)); > > Now if we look inside vm_insert_range(): > > +int vm_insert_range(struct vm_area_struct *vma, unsigned long addr, > + struct page **pages, unsigned long page_count) > +{ > + unsigned long uaddr = addr; > + int ret = 0, i; > + > + if (page_count > vma_pages(vma)) > + return -ENXIO; > + > + for (i = 0; i < page_count; i++) { > + ret = vm_insert_page(vma, uaddr, pages[i]); > + if (ret < 0) > + return ret; > + uaddr += PAGE_SIZE; > + } > > So, page_count _is_ vma_pages(vma). So this code does these operations: > > if (vma_pages(vma) > vma_pages(vma)) > return -ENXIO; > > This will always be false. I've already stated in my reply to patch 2 > in paragraph 3 about the uselessness of this test. Agree, this will be always false for this particular/ similar instances. But there are places [3/9], [6/9], [9/9] where page_count is already set and it might be good to just cross check page_count > vma_pages(vma). This was discussed during review of v3 [1/9]. https://patchwork.kernel.org/patch/10716601/ We can discuss again and if not needed it can be removed in v5. > > for (i = 0; i < vma_pages(vma); i++) { > ret = vm_insert_page(vma, uaddr, pages[i]); > > So the loop will iterate over the number of pages that the user requested. > > Now, taking another example. The object is again 8 pages long, so > indexes 0 through 7 in its page array are valid. The user requests > 8 pages at offset 2 into the object. Also as already stated in > paragraph 3 of my reply to patch 2. > > vma_pages(vma) is 8. offset = 2. > > So we end up _inside_ vm_insert_range() with: > > if (8 > 8) > return -ENXIO; > > As stated, always false. > > for (i = 0; i < 8; i++) { > ret = vm_insert_page(vma, vaddr, rk_obj->pages[2 + i]); > > Which means we iterate over rk_obj->pages indicies from 2 through 9 > inclusive. > > Since only 0 through 7 are valid, we have walked off the end of the > array, and attempted to map an invalid struct page pointer - we could > be lucky, and it could point at some struct page (potentially causing > us to map some sensitive page - maybe containing your bank details or > root password... Or it could oops the kernel. Consider the 2nd example. The object is again 8 pages long, so indexes 0 through 7 in its page array are valid. The user requests 8 pages at offset 2 into the object. The original code look like - unsigned long user_count = vma_pages(vma); // 8 unsigned long end = user_count + offset // 8 + 2 = 10 ... for (i = offset (2) ; i < end ( 10) ; i++) { ret = vm_insert_page(vma, uaddr, rk_obj->pages[i]); if (ret) return ret; uaddr += PAGE_SIZE; } we iterate over rk_obj->pages indices from 2 through 9. Does it indicates the actual code have a bug when *offset != 0*.