From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B612C377575 for ; Tue, 17 Feb 2026 19:42:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771357349; cv=none; b=sRQdPAQ44bqWLNd8x2ZSwl5yHprHChABjCd3/4/aC1VPoJJEU84P8XFsH6Dli4EJNRRYB9nsVbvcJG8/2lsp4d/69egXBTta5WiB3HF4Y+6mlqyOz2TWjfcAzr5giSmJqcaMDsyuZnFkTyNhlpyfmH0QwqS6i9e7r3kYFDzblYw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771357349; c=relaxed/simple; bh=oEd0j4HTHXjey6zdICz4VGHhfh1yZYcwmYPbk8qS6ys=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sBPDNTs2W7gDCVc6x+lNtnO+aQ37wFIbtAbUWA1BIQASWcqo6b/hjZJ2RDSyCvbxRNjU1GoDx4gVLN7gVE7V/yB3sJmxVP+Q+E0LIYke6DqC+KunkCdjw9HsvRJeyQXNs7ozhMRB0P3Z9iG2ByDSGIS0JNSFsuBSYSBQpYVaCAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gXfV8o+3; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gXfV8o+3" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-48373a4bca3so24686095e9.0 for ; Tue, 17 Feb 2026 11:42:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771357346; x=1771962146; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=gPY3U4D8kcI/KJam2ubk4HEothPT8T9Sna2aKUxaL8g=; b=gXfV8o+38o+nh9nCo/1Jhm91+rynNSZaRv2SR1OJfb87PM8ncANt4pEXONDuvGWX9M DWAIog9ZfPF0Mr57PdZwOTpyKSde6m1G1Jn6qLzXq2k/xwZrkHss7CY0FJinuLPnFMP9 kjxQ/gf0fBoiJydhow3z7zY2042xQQvtFZj+vTf+sQJV80g15Tkm55ycZYBqRjUoKU1i eggWdxDpKwz0Tnr2yHjO+NYbrIgBS6rp17s7k2+CfRgpMbOUln3uql6o5olu0/X35QJG js/ucoMZ3v3rQf3OrTcCqsoVcatnc4bhUFdNNLntneeTtFkrbDCsQsYwPW0x8PCRzLCZ CX+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771357346; x=1771962146; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gPY3U4D8kcI/KJam2ubk4HEothPT8T9Sna2aKUxaL8g=; b=pFlmfCqBA5IONeEJHUisT1LJCF35mAMkmlaHCWBLn3fpFNALUycSpxdEWXWwLjF3v1 vlFrSEvIYAQwKOb4UWX7z5Y1gbrKvK1JMSq52sFaCwZPiCMNfviLTK2+ujIC6aZbUgx4 PX4S/KmAMnk4TZk0A9E8bv3ySu3vJvMbaNUOHKrQLnVmh1ozDK6TI978SafSRXNS9XNa hp7Qn7DuFyZShXU+8F9a6BkGHcdsc0pHPX2jszDCHuXlw0KTXti2KEfSXYrV/5EP0JWa ULSaCO+tqs3BjWugvaaDrFlQXDiDUDmvLUHhUyh/hA9hpUkknzsZVTNugabn7+40CC5Q 5fWQ== X-Forwarded-Encrypted: i=1; AJvYcCV4C4XxpJJ6bfnr9hYhj6khkiqvw95dM75AhbQsdRg3Wv0UcK5Ye2kEYXXItqJ0dE6UFzv7lidD9rqXS9E=@vger.kernel.org X-Gm-Message-State: AOJu0YxcmKW7G/bLSEW8T7BzUQAjHkqXwZVH/WVOemVmfYXCzX8OHos0 HATCPaeLSxr6qzVi3KuVmpY6g4gUYUL4jCUA2RL//b0t9FKQoLDPAqaq/BKc0oO57Q== X-Gm-Gg: AZuq6aIsvX8f0/4MZWkOkHMz6BxDqf6JaCQtq6KBxXDTlAjHaKWi0KVV9qB4fW6pkKR eG9lzOMFoac80yN6Pc7csMXuesjbKeLwItSoHzjzHBrLlIidFook+Asyk7Xz3mZ0s67LOYiuLzj mJeUWlTqT36WU0L0hCeOV804WP6zRRETsWpsWAdUyYvPAHXhxL9TXfT98QmeBhQ25Qyymhd4mCK vEZOJAiNtGu8oSH51pHtc4fv5wgbcNS1ubvFbCZEcfciEwN/ZQef8hDpQzuN/XtMLyPfAW5hvgQ SIl19asQguK1inkRr/U0aoOIQUdWmG1OydkUJOlkUMgvVWHl81FISON+oOx1uwfc5QD8aU+Fj5a ZSMat0IIkX54x5KGsDCAVMsxdJRjOWv3iqNWyUg/w5Y551vORRCJf8woXhe/YbsIW+7W7IKZFEY tsqxjOPJMp5K0toH1L7pngcyqRRtgvqNXO4XmZ1VZNbSSHz5fbhJleRg== X-Received: by 2002:a05:600c:1c08:b0:47a:8cce:2940 with SMTP id 5b1f17b1804b1-48379b991c6mr198726345e9.14.1771357345552; Tue, 17 Feb 2026 11:42:25 -0800 (PST) Received: from google.com ([2a00:79e0:288a:8:8d29:f905:4a47:1dbf]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4835dcfafcdsm822528585e9.9.2026.02.17.11.42.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Feb 2026 11:42:24 -0800 (PST) Date: Tue, 17 Feb 2026 20:42:20 +0100 From: =?utf-8?Q?G=C3=BCnther?= Noack To: Benjamin Tissoires Cc: Jiri Kosina , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/3] HID: apple: avoid memory leak in apple_report_fixup() Message-ID: References: <20260217160125.1097578-1-gnoack@google.com> <20260217160125.1097578-2-gnoack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Hello Benjamin! On Tue, Feb 17, 2026 at 07:22:20PM +0100, Benjamin Tissoires wrote: > On Feb 17 2026, Günther Noack wrote: > > The apple_report_fixup() function was allocating a new buffer with > > kmemdup() but never freeing it. Since the caller of report_fixup() already > > provides a writable buffer and allows returning a pointer within that > > buffer, we can just modify the descriptor in-place and return the adjusted > > pointer. > > > > Assisted-by: Gemini-CLI:Google Gemini 3 > > Signed-off-by: Günther Noack > > --- > > drivers/hid/hid-apple.c | 4 +--- > > 1 file changed, 1 insertion(+), 3 deletions(-) > > > > diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c > > index 233e367cce1d..894adc23367b 100644 > > --- a/drivers/hid/hid-apple.c > > +++ b/drivers/hid/hid-apple.c > > @@ -686,9 +686,7 @@ static const __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc, > > hid_info(hdev, > > "fixing up Magic Keyboard battery report descriptor\n"); > > *rsize = *rsize - 1; > > - rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL); > > - if (!rdesc) > > - return NULL; > > + rdesc = rdesc + 1; > > I might be wrong, but later we call free(dev->rdesc) on device removal. > AFAICT, incrementing the pointer is undefined behavior. > > What we should do instead is probably a krealloc instead of a kmemdup. > > Same for all 3 patches. Thanks for the review. Let me try to address your three responses in one reply. I am happy to accept it if I am wrong about this, but I don't understand your reasoning. (This should go without saying, but maybe is worth reiterating, I would not have sent this if I had not convinced myself independently of LLM-assisted reasoning.) Let me explain my reasoning based on the place where .report_fixup() is called from, which is hid_open_report() in hid-core.c: start = device->bpf_rdesc; /* (1) */ if (WARN_ON(!start)) return -ENODEV; size = device->bpf_rsize; if (device->driver->report_fixup) { /* * device->driver->report_fixup() needs to work * on a copy of our report descriptor so it can * change it. */ __u8 *buf = kmemdup(start, size, GFP_KERNEL); /* (2) */ if (buf == NULL) return -ENOMEM; start = device->driver->report_fixup(device, buf, &size); /* (3) */ /* * The second kmemdup is required in case report_fixup() returns * a static read-only memory, but we have no idea if that memory * needs to be cleaned up or not at the end. */ start = kmemdup(start, size, GFP_KERNEL); /* (4) */ kfree(buf); /* (5) */ if (start == NULL) return -ENOMEM; } device->rdesc = start; device->rsize = size; This function uses a slightly elaborate scheme to call .report_fixup: (1) start is assigned to the original device->bpf_rdesc (2) buf is assigned to a copy of the 'start' buffer (deallocated in (5)). . It is done because buf is meant to be mutated by .report_fixup() . (3) start = ...->report_fixup(..., buf, ...) . (4) start = kmemdup(start, ...) (5) deallocate buf Importantly: (a) The buffer buf passed to report_fixup() is a copy of the report descriptor whose lifetime spans only from (2) to (5). (b) The result of .report_fixup(), start, is immediately discarded in (4) and reassigned to the start variable again. >From (b), we can see that the result of .report_fixup() does *not* get deallocated by the caller, and thus, when the driver wants to return a newly allocated array, is must also hold a reference to it so that it can deallocate it later. >From (a), we can see that the report_fixup hook is free to manipulate the contents of the buffer that it receives, but if we were to *reallocate* it within report_fixup, as you are suggesting above, it could become a double free: * During realloc, the allocator would potentially have to move the buffer to a place where there is enough space, freeing up the old place and allocating a new place. [1] * In (5), we would then pass the original (now deallocated) buf pointer to kfree, leading to a double free. If I were to describe the current interface of the hook .report_fixup(dev, rdesc, rsize), it would be: * report_fixup may modify rdesc[0] to rdesc[rsize-1] * report_fixup may *not* deallocate rdesc (ownership of rdesc stays with the caller) * specifically, it may also not reallocate rdesc (because that may imply a deallocation) * report_fixup returns a pointer to a buffer for which it can guarantee that it lives long enough for the kmemdup in (4), but which will *not* be deallocated by the caller (see (b) above). The three techniques I have found for that are: * returning a subsection of the rdesc that it received * returning a pointer into a statically allocated array (e.g. motion_fixup() and ps3remote_fixup() in hid-sony.c) * allocating it with a devm_*() function. My understanding was that this ties it to the lifetime of the device. (e.g. the QUIRK_G752_KEYBOARD case in hid-asus.c) Honestly, I still think that this reasoning holds, but I am happy to be convinced otherwise. Please let me know what you think. —Günther