1686750800.v13-3-g8dbbed1.fix-race-condition-when-creating-rotating-keys-123.patch 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Subject: Fix race condition when creating/rotating keys (#123)
  2. Origin: upstream, v13-3-g8dbbed1 <https://github.com/latchset/tang/commit/v13-3-g8dbbed1>
  3. Upstream-Author: Sergio Correia <scorreia@redhat.com>
  4. Date: Wed Jun 14 10:53:20 2023 -0300
  5. When we create/rotate keys using either the tangd-keygen and
  6. tangd-rotate-keys helpers, there is a small window between the
  7. keys being created and then the proper ownership permissions being
  8. set. This also happens when there are no keys and tang creates a
  9. pair of keys itself.
  10. In certain situations, such as the keys directory having wide open
  11. permissions, a user with local access could exploit this race
  12. condition and read the keys before they are set to more restrictive
  13. permissions.
  14. To prevent this issue, we now set the default umask to 0337 before
  15. creating the files, so that they are already created with restrictive
  16. permissions; afterwards, we set the proper ownership as usual.
  17. Issue reported by Brian McDermott of CENSUS labs.
  18. Fixes CVE-2023-1672
  19. Reviewed-by: Sergio Arroutbi <sarroutb@redhat.com>
  20. Signed-off-by: Sergio Correia <scorreia@redhat.com>
  21. --- a/src/keys.c
  22. +++ b/src/keys.c
  23. @@ -17,6 +17,7 @@
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. +#include <sys/stat.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <dirent.h>
  30. @@ -304,6 +305,9 @@
  31. const char** hashes = supported_hashes();
  32. const char* alg[] = {"ES512", "ECMR", NULL};
  33. char path[PATH_MAX];
  34. +
  35. + /* Set default umask for file creation. */
  36. + umask(0337);
  37. for (int i = 0; alg[i] != NULL; i++) {
  38. json_auto_t* jwk = jwk_generate(alg[i]);
  39. if (!jwk) {
  40. --- a/src/tangd-keygen
  41. +++ b/src/tangd-keygen
  42. @@ -27,6 +27,9 @@
  43. [ $# -eq 3 ] && sig=$2 && exc=$3
  44. +# Set default umask for file creation.
  45. +umask 0337
  46. +
  47. jwe=`jose jwk gen -i '{"alg":"ES512"}'`
  48. [ -z "$sig" ] && sig=`echo "$jwe" | jose jwk thp -i-`
  49. echo "$jwe" > $1/$sig.jwk
  50. --- a/src/tangd-rotate-keys
  51. +++ b/src/tangd-rotate-keys
  52. @@ -72,6 +72,10 @@
  53. # Create a new set of keys.
  54. DEFAULT_THP_HASH="S256"
  55. +
  56. + # Set default umask for file creation.
  57. + umask 0337
  58. +
  59. for alg in "ES512" "ECMR"; do
  60. json="$(printf '{"alg": "%s"}' "${alg}")"
  61. jwe="$(jose jwk gen --input "${json}")"