Skip to content

Remove all strcpy() uses in favor of strscpy() #88

Open

Description

@kees

A lot of kernel code is still using strcpy(). While the CONFIG_FORTIFY_SOURCE wrapper macros tend to make its use mostly safe, it would be nice to eliminate the function from the kernel entirely.

https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

Some background on stracpy() and its rejection.

Activity

  1. changed the title [-]Deprecate strcpy() in favor of strscpy()[/-] [+]Remove all strcpy() uses in favor of strscpy()[/+] on Aug 11, 2020
  2. added
    good first issue
    on Aug 11, 2020
  3. alexdewar commented on Aug 24, 2020

    @alexdewar

    I'm interested in having a go at this, but I was just wondering what you think would be the cleanest way to deal with strcpy(str, "constant string"), where we know that sizeof(str) > sizeof("constant string") will always hold?

  4. kees commented on Aug 1, 2021

    @kees
    Author

    I'm interested in having a go at this, but I was just wondering what you think would be the cleanest way to deal with strcpy(str, "constant string"), where we know that sizeof(str) > sizeof("constant string") will always hold?

    Normally this shouldn't be a problem. Prior to v5.11, under CONFIG_FORTIFY_SOURCE=y a compile-time error will be generated. For example:

           char dst[8];
           ...
           strcpy(dst, "Crash point unregistered\n"); /* line 21 noted below */
    

    produces:

    ...
                     from drivers/misc/lkdtm/core.c:21:
    In function 'memcpy',
        inlined from 'lkdtm_module_exit' at ./include/linux/fortify-string.h:281:2:
    ./include/linux/fortify-string.h:185:4: error: call to '__write_overflow' declared with attribute error: detected write beyond size of object passed as 1st parameter
      185 |    __write_overflow();
          |    ^~~~~~~~~~~~~~~~~~
    

    v5.11 and later seem to have a bug here when strscpy() coverage was added. The runtime check is correct, but the compile time error has gone missing. This needs fixing.

  5. added
    [Refactor] strcpy
    on Apr 7, 2022
  6. 257 remaining items

  7. JustinStitt commented on Oct 19, 2026

    @JustinStitt
    Collaborator

    Keep open In progress

    Note

    Part of an October 2026 triage of every open KSPP issue. The research was AI-assisted (Claude Opus 5.5 investigated and searched the mailing lists; Claude Haiku 4.5 summarized), and I reviewed this summary before posting. Kernel state is as of mainline d24e8ac715de (v7.3-rc5+) and next-20261001.

    Status as of 2026-10-01: Still valid and actively progressing; strcpy() sites dropped from 1948 (v5.9) to 568 in mainline and 555 in linux-next.

    The issue asks to remove all strcpy() calls in the kernel (outside tools/ and scripts/) by replacing them with strscpy() or memcpy(). This treewide refactor is healthy, moving fast, and still fits the "good first issue" label. Mainline commits over the past year average 30 to 40 conversions per release. The remaining ~555 call sites concentrate in drivers/net (117: tg3.c with 21 alone, qed_debug.c 11, mwifiex), drivers/scsi (88: myrb, qla2xxx, bfa, esas2r, csiostor), drivers/video/fbdev (53), drivers/acpi/acpica (16, needs upstream ACPICA sync), drivers/gpu (17), drivers/mtd (15), and init/main.c (10). checkpatch already warns on new uses.

    Evidence

    • d722f1e06fbc ("drivers/iio: Remove all strcpy() uses", v5.16-rc1): early bulk conversion showing the pattern works.
    • 0bdbce25855f ("ALSA: hda: Use safer strscpy() instead of strcpy()", v6.17-rc1): part of the 600-site ALSA sweep that drove the v6.17 drop.
    • aa264d9511aa ("nios2: Replace strcpy() with strscpy() and simplify setup_cpuinfo()", v6.16-rc1): recent mainline patch exemplifying the pattern.
    • Call site count: v5.9 (1948) to v6.16 (1470) to v6.17 (852) to mainline d24e8ac (568), with 309 commits since v6.16 by Takashi Iwai, Thorsten Blum, David Laight and others.

    Mailing list

    • No blocking series. About 20 patches since 2026-07 are mostly merged in v7.3 or queued in linux-next. Key caveat: David Laight and Jani Nikula have NAKed mechanical conversions of strcpy(fixed_array, "literal") to strscpy(), because FORTIFY makes the strcpy form fail at compile time on overflow while strscpy silently truncates. Newcomers should target call sites where destination size or source is not compile-time known, or use the 2-argument strscpy() or memcpy() patterns.

    Suggested next step

    • Refresh the issue description with current call-site counts by subsystem (especially drivers/net, scsi, fbdev, acpica) and add the list of high-count files (tg3.c, qed_debug.c, myrb.c, qla2xxx, bfa, esas2r, csiostor) to guide new contributors. Point them toward sites where the size is not known at compile time or where return value checks are needed, to avoid the FORTIFY gotcha. For drivers/acpi/acpica, route patches to upstream ACPICA instead. Keep the label "good first issue" as the refactor is still one driver or subsystem per patch.

    Full investigation: https://github.com/KSPP/bug-scrub-archaeology/tree/master/issues/issue-88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Refactor] strcpygood first issuekeep_openstatus_in_progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions