0007-libmount-fix-x-options-use-for-non-root-users.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. From 88f04bf9e637b14ff5274e4c4981e43ab2c40c2b Mon Sep 17 00:00:00 2001
  2. From: Karel Zak <kzak@redhat.com>
  3. Date: Tue, 28 Jan 2020 10:45:07 +0100
  4. Subject: [PATCH] libmount: fix x- options use for non-root users
  5. libmount returns EPERM for all X- and x- mount options for non-root
  6. users when evaluate X-mount.mkdir. It's bug, we need to be sensitive
  7. to only X-mount.mkdir and only if the target directory is missing.
  8. Addresses: https://github.com/karelzak/util-linux/issues/941
  9. Signed-off-by: Karel Zak <kzak@redhat.com>
  10. ---
  11. libmount/src/context.c | 41 ++++++++++++++++++++++-------------------
  12. sys-utils/mount.8 | 6 +++---
  13. 2 files changed, 25 insertions(+), 22 deletions(-)
  14. diff --git a/libmount/src/context.c b/libmount/src/context.c
  15. index 6c536cc15..91fe8e4bf 100644
  16. --- a/libmount/src/context.c
  17. +++ b/libmount/src/context.c
  18. @@ -1855,49 +1855,48 @@ end:
  19. return rc;
  20. }
  21. -/* create a mountpoint if X-mount.mkdir[=<mode>] specified */
  22. -static int mkdir_target(const char *tgt, struct libmnt_fs *fs)
  23. +static int is_mkdir_required(const char *tgt, struct libmnt_fs *fs, mode_t *mode, int *rc)
  24. {
  25. char *mstr = NULL;
  26. size_t mstr_sz = 0;
  27. - mode_t mode = 0;
  28. struct stat st;
  29. - int rc;
  30. assert(tgt);
  31. assert(fs);
  32. + assert(mode);
  33. + assert(rc);
  34. +
  35. + *mode = 0;
  36. + *rc = 0;
  37. if (mnt_optstr_get_option(fs->user_optstr, "X-mount.mkdir", &mstr, &mstr_sz) != 0 &&
  38. mnt_optstr_get_option(fs->user_optstr, "x-mount.mkdir", &mstr, &mstr_sz) != 0) /* obsolete */
  39. return 0;
  40. - DBG(CXT, ul_debug("mkdir %s (%s) wanted", tgt, mstr));
  41. -
  42. if (mnt_stat_mountpoint(tgt, &st) == 0)
  43. return 0;
  44. + DBG(CXT, ul_debug("mkdir %s (%s) wanted", tgt, mstr));
  45. +
  46. if (mstr && mstr_sz) {
  47. char *end = NULL;
  48. errno = 0;
  49. - mode = strtol(mstr, &end, 8);
  50. + *mode = strtol(mstr, &end, 8);
  51. if (errno || !end || mstr + mstr_sz != end) {
  52. DBG(CXT, ul_debug("failed to parse mkdir mode '%s'", mstr));
  53. - return -MNT_ERR_MOUNTOPT;
  54. + *rc = -MNT_ERR_MOUNTOPT;
  55. + return 0;
  56. }
  57. }
  58. - if (!mode)
  59. - mode = S_IRWXU | /* 0755 */
  60. + if (!*mode)
  61. + *mode = S_IRWXU | /* 0755 */
  62. S_IRGRP | S_IXGRP |
  63. S_IROTH | S_IXOTH;
  64. - rc = mkdir_p(tgt, mode);
  65. - if (rc)
  66. - DBG(CXT, ul_debug("mkdir %s failed: %m", tgt));
  67. -
  68. - return rc;
  69. + return 1;
  70. }
  71. int mnt_context_prepare_target(struct libmnt_context *cxt)
  72. @@ -1905,6 +1904,7 @@ int mnt_context_prepare_target(struct libmnt_context *cxt)
  73. const char *tgt, *prefix;
  74. int rc = 0;
  75. struct libmnt_ns *ns_old;
  76. + mode_t mode = 0;
  77. assert(cxt);
  78. assert(cxt->fs);
  79. @@ -1946,12 +1946,15 @@ int mnt_context_prepare_target(struct libmnt_context *cxt)
  80. /* X-mount.mkdir target */
  81. if (cxt->action == MNT_ACT_MOUNT
  82. && (cxt->user_mountflags & MNT_MS_XCOMMENT ||
  83. - cxt->user_mountflags & MNT_MS_XFSTABCOMM)) {
  84. + cxt->user_mountflags & MNT_MS_XFSTABCOMM)
  85. + && is_mkdir_required(tgt, cxt->fs, &mode, &rc)) {
  86. /* supported only for root or non-suid mount(8) */
  87. - if (!mnt_context_is_restricted(cxt))
  88. - rc = mkdir_target(tgt, cxt->fs);
  89. - else
  90. + if (!mnt_context_is_restricted(cxt)) {
  91. + rc = mkdir_p(tgt, mode);
  92. + if (rc)
  93. + DBG(CXT, ul_debug("mkdir %s failed: %m", tgt));
  94. + } else
  95. rc = -EPERM;
  96. }
  97. diff --git a/sys-utils/mount.8 b/sys-utils/mount.8
  98. index 3e57251ed..698b0f011 100644
  99. --- a/sys-utils/mount.8
  100. +++ b/sys-utils/mount.8
  101. @@ -1306,14 +1306,14 @@ functionality have been extended to keep existing fstab configurations usable
  102. without a change.
  103. .TP
  104. .BR X-mount.mkdir [ = \fImode\fR ]
  105. -Allow to make a target directory (mountpoint). The optional argument
  106. +Allow to make a target directory (mountpoint) if it does not exit yet.
  107. +The optional argument
  108. .I mode
  109. specifies the filesystem access mode used for
  110. .BR mkdir (2)
  111. in octal notation. The default mode is 0755. This functionality is supported
  112. only for root users or when mount executed without suid permissions. The option
  113. -is also supported as x-mount.mkdir, this notation is deprecated for mount.mkdir
  114. -since v2.30.
  115. +is also supported as x-mount.mkdir, this notation is deprecated since v2.30.
  116. .SH "FILESYSTEM-SPECIFIC MOUNT OPTIONS"
  117. You should consult the respective man page for the filesystem first.
  118. --
  119. 2.18.2