From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752266AbbJEQWb (ORCPT ); Mon, 5 Oct 2015 12:22:31 -0400 Received: from mail-wi0-f170.google.com ([209.85.212.170]:38131 "EHLO mail-wi0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750979AbbJEQW3 (ORCPT ); Mon, 5 Oct 2015 12:22:29 -0400 Date: Mon, 5 Oct 2015 18:22:26 +0200 From: Ingo Molnar To: Linus Torvalds Cc: Alexey Dobriyan , Chris Metcalf , Linux Kernel Mailing List Subject: Re: [PATCH] string: Improve the generic strlcpy() implementation Message-ID: <20151005162226.GA10993@gmail.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Linus Torvalds wrote: > > 2) strscpy() will copy garbage past NUL from source into destination. It won't > > fault but still, who knows what lies after string. > > Yes, that's probably worth fixing before we get actual users.. Hm, this is the spot: c = *(unsigned long *)(src+res); *(unsigned long *)(dest+res) = c; if (has_zero(c, &data, &constants)) { data = prep_zero_mask(c, data, &constants); data = create_zero_mask(data); return res + find_zero(data); } We could do something like: c = *(unsigned long *)(src+res); *(unsigned long *)(dest+res) = c; if (has_zero(c, &data, &constants)) { unsigned int zero_pos; data = prep_zero_mask(c, data, &constants); data = create_zero_mask(data); zero_pos = find_zero(data); res += zero_pos; memset(dest+res, 0, sizeof(long)-zero_pos); return res; } I.e. the extra memset() clears out the partial word (if any) after the NUL. Completely untested. Thanks, Ingo