From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5989538E; Sat, 8 Oct 2022 18:16:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665252990; x=1696788990; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q7YlJwn22ePcF+UsrTEOpg8IcVNJxX9fgtN3uSim9/E=; b=DJJ/YFV9BD6sqCsrme7A79tkpZG6OVOiyvMc8EUO8eWH2EJCeYZfVPYn 9iT848V6VJAnQHvXtrxOkCX2JnVFbTpjrBJ54OSKupu61TaUoIzxkgODr apizg8o/669DIcA5Ivz/COaQOlZGuGJDCcYjNLwSK//4OxRWMtsrXduJb Yah2tVmQee2V2zj6hcE9nhRPtiJQh5CNCqidhcKDpIH9mYqD8rPy3TJJk xheJ5IuUjFwYho/nNRuG5uqdj1jllySl1XZ777sSOf4I4pM1BxksL/D6u IGokcabyQ2MDPwzFWzszMUn70kMCSj1F0cyXFFFCbWb7J3bFYGzEgTrwE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="365920526" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="365920526" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2022 11:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="750899918" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="750899918" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga004.jf.intel.com with ESMTP; 08 Oct 2022 11:16:23 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1ohEN3-0048ft-0Q; Sat, 08 Oct 2022 21:16:21 +0300 Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, patches@lists.linux.dev, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko