Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 18154C54E58 for ; Wed, 20 Mar 2024 20:27:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Wdi+OCgA74tdx+HJvtF9Zlx26wKeRiGirfZPh17mWo8=; b=R3HlzN7f7AC4Dv dnQH+hE02EwgR8WoVpzz1dVbdYx40C2kZhhjcmZ5rJl1gWEqJufV0qemj9Sgnr2r9kkXg9tnD5Y+z Jj/RMvm4cHLmi39W47pRwF1QYFXejBaWoK8U3ZovRoCSPZkx3IKLx5dJTPlQPAZElSQty6Cr6IAp1 fPG5mbf9QsJAEnc2M1zQDR1e2L6aBtEbg+RjzGuRrUyqLEWB76zjNQNr8llWz3iy2R+24eUyb7rIe TkrUD30nPRan6CjvuS7wyjLhqH9o+Y3kQkQcodEfPrZeqFOPKwop6KPdYcmjuVMCjQO1VRVhX1KLW yxbxV1RGq8e1NxfCQa6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rn2WN-00000000tJm-0Sfw; Wed, 20 Mar 2024 20:26:47 +0000 Received: from mail-qv1-xf30.google.com ([2607:f8b0:4864:20::f30]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rn2WJ-00000000tJF-3zvv for linux-arm-kernel@lists.infradead.org; Wed, 20 Mar 2024 20:26:45 +0000 Received: by mail-qv1-xf30.google.com with SMTP id 6a1803df08f44-696147fb5a7so2785366d6.0 for ; Wed, 20 Mar 2024 13:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710966402; x=1711571202; darn=lists.infradead.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=JJHOdOKJUmfqeQbZlrKNsW4i7vp0L3XS76AWUl4dGTE=; b=YncsXegzFc7ptaY3rOwTBwAHlNHL+s1VNe0dfv/g06xw2PTrJr5hL7W3o25icJf/EE EOk6Kuq9e9C29c3Zp3EEyqTMGJrErtFYSgwIrJLLGPWghsXMt78tHKxbbhkl36ivNWI4 NUbdlkfkWwBZ9UiVagzSAeUdCSIkpMR49VwCOxKhqcJJJS3ZvhOJyDvpUMVhlYD8t7vi Vc9D9EIjWKlw/4wu18kV/eesG/Sc9TqMAjLhgV+9wV2x/WMg6lY5B2PS67lxRKO5uFhO +ihCFM0Xsvm2Qr9LInQHSkDBiyLXRK2nsuSxzJnDcx12B0bKg8HxWsvsvL6rHapr5+ro oXNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710966402; x=1711571202; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JJHOdOKJUmfqeQbZlrKNsW4i7vp0L3XS76AWUl4dGTE=; b=X3w2PuWCl3MYekfg05XCwGXVh7YVlBRuY0KeNwe7Q9wR0PLUSc9GDTcbIjiNQvOiJL vD3n0nZsRnnOZhsNDK6PXbl1o3SZBqvTwzXE5E4XR2KfFA+7O0wkC29BlCKoFy9Bgvne Y0H7CKgrDQNAgDtVGtAItFWiYqIBY8/pW38k+/4isy/R4DqizQSdR12l8/is+4YfTznm 5Fat/+93Vy/Cv/TBEU3I1F8O0srBoYWZYBfUY644d/U4IFX7XroD69k2oOyzoEwik2Qg 3PgJkwG1cr7qTEWXblmBwaf8u190S0a1XQN1sFruA5ETfG0ao00dcdqWMwkEMohpwli7 PMQw== X-Forwarded-Encrypted: i=1; AJvYcCVgTOu+6suPipCznSImBhnySHN0x0XtE+bGvr4cP9+pNU8K4XLuEcb0EY6BMwXtNjOXqrmMRrnDxexM34jNc1tJQP3n26/XHZ73o21mfXIv978ClUs= X-Gm-Message-State: AOJu0Yy4wOjPkG3B3p+DpGSNA1vfLoAmakuaRQ+H9e0MqgNhVEXAlz+h uWHRRhwLMdPmqzSIDZZtNMLVsMuuZaqerCldk9lulu46NA/C0J/i X-Google-Smtp-Source: AGHT+IG+ZGb6SdZUYkNiS9g8ouRwXmCTkRyLtYV6QqhJ8iOmlvrhnQf8T6b8duiqdOEPxIOs/nuJdA== X-Received: by 2002:a0c:e38c:0:b0:696:46a7:9f93 with SMTP id a12-20020a0ce38c000000b0069646a79f93mr2222963qvl.45.1710966402219; Wed, 20 Mar 2024 13:26:42 -0700 (PDT) Received: from [10.193.190.170] (mobile-130-126-255-72.near.illinois.edu. [130.126.255.72]) by smtp.gmail.com with ESMTPSA id kl13-20020a056214518d00b00690dcc7ae8dsm8184637qvb.3.2024.03.20.13.26.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Mar 2024 13:26:42 -0700 (PDT) Message-ID: Date: Wed, 20 Mar 2024 15:26:41 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [arch/arm/mach-mvebu] Question about kzalloc NULL check in i2c_quirk To: "Russell King (Oracle)" Cc: Andrew Lunn , gregory.clement@bootlin.com, sebastian.hesselbarth@gmail.com, linux-arm-kernel@lists.infradead.org, chenyuan0y@gmail.com References: <1e40d5f4-72ac-40ca-8fb0-34884e7ac549@gmail.com> <2baa9e59-fe81-4c92-b6ca-a86233c5e06d@gmail.com> Content-Language: en-US From: Zijie Zhao In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240320_132644_034528_61D28B30 X-CRM114-Status: GOOD ( 22.17 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 3/20/24 2:46 PM, Russell King (Oracle) wrote: > On Wed, Mar 20, 2024 at 01:48:26PM -0500, zzjas98@gmail.com wrote: >> On 3/20/24 7:27 AM, Andrew Lunn wrote: >>> On Tue, Mar 19, 2024 at 10:03:22PM -0500, Zijie Zhao wrote: >>>> Dear ARM/Marvell maintainers, >>>> >>>> We came across an unusual usage of kzalloc in >>>> arch/arm/mach-mvebu/board-v7.c, function i2c_quirk: >>>> >>>> https://elixir.bootlin.com/linux/v6.8/source/arch/arm/mach-mvebu/board-v7.c#L127 >>>> ``` >>>> static void __init i2c_quirk(void) >>>> { >>>> struct device_node *np; >>>> u32 dev, rev; >>>> >>>> /* >>>> * Only revisons more recent than A0 support the offload >>>> * mechanism. We can exit only if we are sure that we can >>>> * get the SoC revision and it is more recent than A0. >>>> */ >>>> if (mvebu_get_soc_id(&dev, &rev) == 0 && rev > MV78XX0_A0_REV) >>>> return; >>>> >>>> for_each_compatible_node(np, NULL, "marvell,mv78230-i2c") { >>>> struct property *new_compat; >>>> >>>> new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL); >>>> >>>> new_compat->name = kstrdup("compatible", GFP_KERNEL); >>>> new_compat->length = sizeof("marvell,mv78230-a0-i2c"); >>>> new_compat->value = kstrdup("marvell,mv78230-a0-i2c", >>>> GFP_KERNEL); >>>> >>>> of_update_property(np, new_compat); >>>> } >>>> } >>>> ``` >>>> >>>> Should the new_compat be checked against NULL in case kzalloc fails, to >>>> avoid NULL dereference later in the code? >>>> >>>> Please kindly let us know if we missed any key information and this is >>>> actually intended. We appreciate your information and time! Thanks! >>> >>> What context is this code run in? What would need to happen for the >>> allocation to fail? And what happens next if it does fail, both in >>> this function and the system in general? >>> >>> Andrew >>> >> >> Hi Andrew, >> >> Thanks for checking! >> >> We encountered this kzalloc while doing a static analysis for the kernel code. >> >> kzalloc would return NULL in case of out-of-memory and would make the next field access new_compat->name segfault. > > If we are out of memory at this point in the kernel boot, then what are > the chances of the kernel getting to the point where it can run > userspace? > > I think that is the essence of Andrew's argument. A failure to allocate > memory here means there is a serious problem and the system won't boot > _even_ if we add something like: > > if (!new_compat) > continue; > > to that loop. > Hi Andrew and Russell, Thanks for pointing out the consequence! It makes sense and we will try to make our analyzer realize this as well. Thanks again for your time! Best, Zijie _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel