Why are strlcpy and strlcat considered insecure?

前端 未结 7 1147
一整个雨季
一整个雨季 2020-11-22 05:49

I understand that strlcpy and strlcat were designed as secure replacements for strncpy and strncat. However, some people

7条回答
  •  無奈伤痛
    2020-11-22 06:29

    I don't think strlcpy and strlcat are consider insecure or it least it isn't the reason why they're not included in glibc - after all, glibc includes strncpy and even strcpy.

    The criticism they got was that they are allegedly inefficient, not insecure.

    According to the Secure Portability paper by Damien Miller:

    The strlcpy and strlcat API properly check the target buffer’s bounds, nul-terminate in all cases and return the length of the source string, allowing detection of truncation. This API has been adopted by most modern operating systems and many standalone software packages, including OpenBSD (where it originated), Sun Solaris, FreeBSD, NetBSD, the Linux kernel, rsync and the GNOME project. The notable exception is the GNU standard C library, glibc [12], whose maintainer steadfastly refuses to include these improved APIs, labelling them “horribly inefficient BSD crap” [4], despite prior evidence that they are faster is most cases than the APIs they replace [13]. As a result, over 100 of the software packages present in the OpenBSD ports tree maintain their own strlcpy and/or strlcat replacements or equivalent APIs - not an ideal state of affairs.

    That is why they are not available in glibc, but it is not true that they are not available on Linux. They are available on Linux in libbsd:

    • https://libbsd.freedesktop.org/

    They're packaged in Debian and Ubuntu and other distros. You can also just grab a copy and use in your project - it's short and under a permissive license:

    • http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/strlcpy.c?rev=1.11

提交回复
热议问题