Netgear / wsdd2

WSD/LLMNR Discovery/Name Service Daemon
GNU General Public License v3.0
154 stars 33 forks source link

Correct usage of strncpy #14

Open tripplet opened 3 years ago

tripplet commented 3 years ago

Adding the debian CFLAGS option in the Makefile for improved security

CFLAGS = -g -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security

results in a warning from the strncpy call here https://github.com/Andy2244/wsdd2/blob/3b2a6a476b47822e7231a0f209c725489da50801/wsdd2.c#L396

In file included from /usr/include/string.h:519,
                 from wsdd.h:30,
                 from wsdd2.c:23:
In function ‘strncpy’,
    inlined from ‘open_ep’ at wsdd2.c:396:3:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 15 bytes from a string of length 15 [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Context of the reported error: https://github.com/Andy2244/wsdd2/blob/3b2a6a476b47822e7231a0f209c725489da50801/wsdd2.c#L393-L404

ifr.ifr_name is of size IFNAMSIZ and assuming ep->ifname also a size of IFNAMSIZ.

The call should be

strncpy(ifr.ifr_name, ep->ifname, IFNAMSIZ); 

Reference https://en.cppreference.com/w/c/string/byte/strncpy

I'm not sure if this is the best approach or if a fix is even necessary as this would only lead to a problem if ep->ifname has exactly IFNAMSIZ-1 characters followed by the terminating 0 character:

Example: http://cpp.sh/6xxiz