Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4343167pxb; Sat, 6 Nov 2021 11:52:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy77pSZA8JAAEuncG4NdZZs5/svupTqwTwgNCLezERX5asLWTmvGyvgrjc/VRhbbMmt2mio X-Received: by 2002:a5d:9ed6:: with SMTP id a22mr5409699ioe.167.1636224775567; Sat, 06 Nov 2021 11:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636224775; cv=none; d=google.com; s=arc-20160816; b=CfwPzKn3onCyImrRLqMeh7OUtTdZZo/d/fTrMYJ8CV6LkhM+ywFhpBI0nB4Q0rfvJZ 4gAKz0maQ0Rt0Tle5I7hfPchWtlC2uXcEjPjZ17ZxoR/fawUynSa2cxHBnkMRg3+Koka EIC+CLmcSyMZzICkHBQHvO9ZDbnCXgm0nJoSblET9BLk/AkxDyQ2sZ6I/h0fLxVXjDFn Ek0urSqzGafCS0Ukgkrs4QB7Efnd+1yhK4MuPb0YepHCw5GHPRvcg1qN9z3l3ZKPF31p EfcxRB4+02igDyqunsPNDu8ONOfauV5hcz2XypV0AZroxB2caJOc3AWVqsH8iKg+HmoU wZ7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=NQeZyi95jWJGOsuGRgyHzDyg1Fg28fFWiAntEq83cYY=; b=SpK4Zjkl7/viNEwClJMuGVX1P6M0a8R3OdsmutUhudrDhLE7Ws4XQtkiCMY7NAN+B8 G3A9ioK374kCKXm/uINboK8bLxb2X4YvCLDESxkQa18uXFDG6hZQrf1QVAxnARHf+WOL eVXmOu4yDD/1XE0dQRMYCnlrYT0iTOy8Ru3YREW6bkPc1TugltWX96W10lVpuYU0yVlX RYuU4pY8O0WchXAr5uRDZDjFamMR7DsPA8j0gD9jEjraG/vOsv3TF74isM54Wchrvv7r 6D7i3tfLhj2y/QMAAy7rhhaD3Zf/Me6eOrIDrzfyll8XuNIjInxFdcGvefCOSMaRXd57 fP9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SwDBBd8x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id h22si15311091jal.57.2021.11.06.11.52.42; Sat, 06 Nov 2021 11:52:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SwDBBd8x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234142AbhKFMIp (ORCPT + 99 others); Sat, 6 Nov 2021 08:08:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234105AbhKFMIo (ORCPT ); Sat, 6 Nov 2021 08:08:44 -0400 Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 887C9C061714; Sat, 6 Nov 2021 05:06:03 -0700 (PDT) Received: by mail-ot1-x32c.google.com with SMTP id v40-20020a056830092800b0055591caa9c6so17267065ott.4; Sat, 06 Nov 2021 05:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NQeZyi95jWJGOsuGRgyHzDyg1Fg28fFWiAntEq83cYY=; b=SwDBBd8xITup5COq/PlmYeNl9XWxgsLKWKFHJzIVG5DfFKDv5eIgAQ34JMxfi5JZI2 pcsI/fgxL7hn6MHwCQ9a/NTleRnfxhnM4++jzK9VBwKEFV1bceef8MYVewX1+4cuyFcT RBuWGyNarcNYDx0JYTZ7jFPFgnbBYSU7wHcGNS/dAVlojDVkz4tEgLGswSQPiagTFkWp fIsy07ds0d+IcQgRybErvnIWJ/1RfjlrUYCkPMC34Kn2Y/f8SW0bVml2okp3DVXIw7aB IJDQU1j2KykPrErDUlVoEHXPaZj95F1Z5oE9ppArqDfY3HCTCcxnb93AtTUSYr9emrsH KUBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NQeZyi95jWJGOsuGRgyHzDyg1Fg28fFWiAntEq83cYY=; b=hvqbVH763kP3dpNhXxRRquZv30P1SXJP+jSuWY3IL3BoH8n4CPcyYLIbQKZhGnaAvz bZLFztk9GgJoovla9I4GylelJJQVyKJKUmElR8hIRzR+2AWXS4ByI14YhKPulSruOi0l D7PA9S7YiLfbMKBBtbU40ReSCvR6Zq5z+c0ZKnIaEAO0Uz4Dww47grg2QeNJvBf6vYhi Ki7vsqWk83ZCuG3gdm+L0XupvE70eFjZ9yKWsjJB0DG3z78Ea7gqPTwaVMuBtcGep/Kn Gutb9P1Dudfe5sDWrqxcREz9fNwkt0x+15GL8v4I655vNUPw6r03wHqLmjnfmzgtTPbe igUA== X-Gm-Message-State: AOAM5303x0dy6BGzhk/PvFo2YVh+Ft0qrqbNxhuBus+YKVexfhp219sc IdXdg7B6HW5dtrkhHF2iM4/o7VEv1aW/wwPMu/oWUJDJFXM= X-Received: by 2002:a9d:7617:: with SMTP id k23mr34752623otl.351.1636200361419; Sat, 06 Nov 2021 05:06:01 -0700 (PDT) MIME-Version: 1.0 References: <20211106092041.43745-1-ajaygargnsit@gmail.com> <9eafae1f-d9f0-298d-cf20-212865d0becc@gmail.com> In-Reply-To: <9eafae1f-d9f0-298d-cf20-212865d0becc@gmail.com> From: Ajay Garg Date: Sat, 6 Nov 2021 17:35:49 +0530 Message-ID: Subject: Re: [PATCH] tty: vt: keyboard: do not copy an extra-byte in copy_to_user To: Pavel Skripkin Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Pavel, Thanks for the review. > > len = strlcpy(kbs, func_table[kb_func] ? : "", len); > > ^^^^^^^^^ > > len is reinitialized here, i.e len passed to kmalloc and len passed to > copy_to_user() can be different. Sorry, I missed this part. > > strlcpy() returns strlen() of source string (2nd argument), that's why > we need +1 here to pass null byte to user. > > Am I missing something? > > Seems things are more screwed. I tried to see the behaviour, via a small program as below : ########################## #include #include char a[10] = {0}; char b[] = "1234567890123456"; int main() { int len = strlcpy(a, b, sizeof(a)); printf("len = [%d]\n", len); printf("a = [%s]\n", a); return 0; } ########################## The result is : ########################## len = [16] a = [123456789] ########################## As seen, len is *not equal* to the number of bytes actually copied. (The bytes actually copied are 9 in number, plus 1 for the terminator, as expected by strlcpy). On re-reading the doc for strlcpy, it seems that strlcpy returns the length of src it "intended* to copy, and not the bytes *actually copied*. If so, then returned value of len is meaningless. So, it seems following two changes should be made in the original code : 1. len = strlcpy(kbs, func_table[kb_func] ? : "", len); => strlcpy(kbs, func_table[kb_func] ? : "", len); 2. ret = copy_to_user(user_kdgkb->kb_string, kbs, len) ? -EFAULT : 0; => ret = copy_to_user(user_kdgkb->kb_string, kbs, strlen(kbs) + 1) ? -EFAULT : 0; In 1, we change to simply not using the returned value of strlcpy. In 2, we change to using strlen(kbs) + 1, as the number of bytes to copy. Kindly know your thoughts. Thanks and Regards, Ajay