Bug 247295

Summary: sbin/md5: sha256 -c with stdin always exits 0
Product: Base System Reporter: sega01
Component: binAssignee: Allan Jude <allanjude>
Status: Closed FIXED    
Severity: Affects Some People CC: allanjude, emaste
Priority: --- Flags: allanjude: mfc-stable12+
allanjude: mfc-stable11-
Version: 12.1-RELEASE   
Hardware: Any   
OS: Any   

Description sega01 2020-06-16 04:42:21 UTC
sha256 (and its cousins) have a -c argument to validate against a given checksum. This works fine on files. Example:

$ echo test > file
$ sha256 file
SHA256 (file) = f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
$ sha256 -c f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 file; echo $?
SHA256 (file) = f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
0
$ sha256 -c dead1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26beef file; echo $?
SHA256 (file) = f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 [ Failed ]
2

However, stdin appears to either be always positive or false positive:

$ cat file | sha256
f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
$ cat file | sha256 -c f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2; echo $?
0
$ cat file | sha256 -c dead1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc2beef; echo $?
0

I think that sha256 -c should give an error or warning with stdin since it is not handled properly, or preferably function as expected. It would be easy to be misled by the current behavior.

Please let me know if you need anything else from me. Thank you!
Comment 1 Kubilay Kocak freebsd_committer freebsd_triage 2020-06-16 05:00:13 UTC
Thank you for the report Teran
Comment 2 commit-hook freebsd_committer freebsd_triage 2020-06-19 19:16:35 UTC
A commit references this bug:

Author: allanjude
Date: Fri Jun 19 19:16:26 UTC 2020
New revision: 362412
URL: https://svnweb.freebsd.org/changeset/base/362412

Log:
  md5(1): fix -c flag to work with input on stdin

  Previously, the -p and -c flags were ignored when reading from stdin
  Additionally, -s and -c can be used together now.

  PR:		247295
  Reviewed by:	kevans
  MFC after:	2 weeks
  Relnotes:	yes
  Sponsored by:	Klara Inc.
  Differential Revision:	https://reviews.freebsd.org/D25362

Changes:
  head/sbin/md5/md5.c
Comment 3 sega01 2020-06-19 20:05:13 UTC
Great, thank you so much! Awesome work!
Comment 4 Kubilay Kocak freebsd_committer freebsd_triage 2020-06-20 02:48:51 UTC
^Triage: Pending merges to stable (?), set to + after merge
Comment 5 commit-hook freebsd_committer freebsd_triage 2020-07-11 14:55:57 UTC
A commit references this bug:

Author: allanjude
Date: Sat Jul 11 14:55:12 UTC 2020
New revision: 363096
URL: https://svnweb.freebsd.org/changeset/base/363096

Log:
  MFC r346369
    Close filedescriptors when done with them.

  MFC r362412
    fix -c flag to work with input on stdin

  PR:		247295
  Sponsored by:	Klara Inc.
  Event:	July 2020 Bugathon

Changes:
_U  stable/12/
  stable/12/sbin/md5/md5.c
Comment 6 Allan Jude freebsd_committer freebsd_triage 2020-07-11 14:57:47 UTC
Merged to stable/12, not merged to stable/11 because it is too far behind, and the last release has already been made.