git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] refs.c: add a function to append a reflog entry to a fd

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 20, 2014, 01:22 UTC
Message-ID
<xmqqzjbm4qe5.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1416444142-28042-1-git-send-email-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
> Compared to the last send of this patch[1], there was one change in the print
> function. Replaced sprintf by snprintf for security reasons. 

Careful. I despise people who blindly think strlcpy() and snprintf() are good solutions for for security. They are by themselves not.

Surely, the use of them lets you avoid stomping on pieces of memory that you did not intend to write to. I'd call that "protecting other people's data".

But what about your own data? If you are thinking that you are trying to write "this and that", but by mistake (either by your or by a careless future change) if you did not allocate enough, you would stop at "this and th". You may have protected other people's data, but your result is still broken. If you gave that to others without checking that you produced an incomplete piece of data, what guarantee are you getting that you are not creating a security hole there (imagine you intended to copy "rm -fr ./tmpdir" and by mistake you stopped at "rm -fr ./" @~@).

Show 9 quoted lines
> +	msglen = msg ? strlen(msg) : 0;
> +	maxlen = strlen(committer) + msglen + 100;
> +	logrec = xmalloc(maxlen);
> +	len = snprintf(logrec, maxlen, "%s %s %s\n",
> +		       sha1_to_hex(old_sha1),
> +		       sha1_to_hex(new_sha1),
> +		       committer);
> +	if (msglen)
> +		len += copy_msg(logrec + len - 1, msg) - 1;

In this codepath, you are allocating enough buffer to hold the whole message; there is no difference between sprintf() and snprintf(). If the difference mattered, you would have chopped the reflog entry too short, and produced a wrong result, but you then discard the whole record (the code that follows the above), losing data.

So use of snprintf() is not really buying you much here, not in the current code certainly, but not as a future-proofing measure, either.

Previous: Stefan BellerNext: Junio C Hamano
Message 2 of 18 in “refs.c: add a function to append a reflog entry to a fd”
  1. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  2. Junio C HamanoNov 20, 2014
  3. Junio C HamanoNov 20, 2014
  4. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  5. Jonathan NiederNov 20, 2014
  6. Junio C HamanoNov 20, 2014
  7. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  8. Jonathan NiederNov 20, 2014
  9. Stefan BellerNov 20, 2014
  10. Jonathan NiederNov 20, 2014
  11. Junio C HamanoNov 20, 2014
  12. Stefan BellerNov 20, 2014
  13. Jonathan NiederNov 20, 2014
  14. Junio C HamanoNov 20, 2014
  15. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  16. Jonathan NiederNov 20, 2014
  17. Junio C HamanoNov 20, 2014
  18. Jonathan NiederNov 20, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.