Skip to contentSkip to conversation

fs: let d_real() fail

PATCH In review Christian Brauner 5 participants 36 messages Nov 16 – Nov 25

Subsystems (18)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

v2 cover fs: let d_real() fail

overlayfs looks up the data of a metacopy file in a data-only layer lazily and validates its verity digest when it's first needed. Both can fail but d_real() has no way to report it. So ovl_d_real() warns and hands back the overlayfs dentry and every caller silently works with an inode that doesn't host the data:

- uprobes get created just fine and fail later with a bogus -EIO. Every new uprobe warns again for each of them. With panic_on_warn that's a panic.
- IMA skips detecting changes to the backing file and hands out a cached verdict.
- IPE evaluates its policy against an inode that has none of the dm-verity or fs-verity properties of the real file.

And that's not limited to files nobody opened yet. A metadata-only copy-up of an open file makes overlayfs validate the digest again.

As Amir pointed out the correct fix is to teach the callers to deal with an error. So d_real() may now fail for D_REAL_DATA and all callers handle it: uprobes fail when they're created, IMA treats it like failing to hash the file, IPE evaluates the file without verity state and the bpf kfunc returns NULL. Then d_real_inode() goes away so nobody can ignore the error again. The first patch only drops the WARN.

This came out of the same work as the freeze protection fixes for backing files (cf. [1]). I ran into it with the pseudo filesystem series (cf. [2]) where file_check_backing() looks at the file hosting the data of O_PATH files registered for fuse passthrough. With this series it can just return the error. But this stands on its own.

[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]

---
Changes in v2:
- Drop the second Fixes: tag from the first patch as per Amir's
suggestion.
- Let d_real_metadata_inode() fail too and handle that in IMA and EVM
as per Amir's suggestion.
- Evaluate a file whose data can't be resolved without any verity state
in IPE instead of denying it as per Fan's suggestion.
- Disable the events before removing the uprobes in the ftrace selftest cleanup, skip it without tmpfs xattr support and add the config
options it needs.
- Link to v1: https://patch.msgid.link/[email protected]

---
Christian Brauner (13):
ovl: don't warn when the lazy lowerdata lookup fails in ovl_d_real()
fs: add d_real_data_inode() and d_real_metadata_inode() tracing/uprobes: handle errors from resolving the real inode
bpf: handle errors from resolving the real inode in uprobe_multi selftests/bpf: handle d_real_data_inode() errors in bpf_testmod
ima: handle errors from resolving the real inode ima,evm: use d_real_metadata_inode()
ipe: handle errors from resolving the real inode
bpf: return NULL from bpf_real_data_inode() on errors
ovl: return the error from the lazy lowerdata lookup in ovl_d_real()
fs: remove d_real_inode()
selftests/ftrace: add uprobe tests on overlayfs selftests/bpf: add uprobe_multi test on overlayfs without data

 Documentation/filesystems/porting.rst              |  10 ++
 Documentation/filesystems/vfs.rst                  |   7 +
 fs/bpf_fs_kfuncs.c                                 |  13 +-
 fs/overlayfs/super.c                               |  19 ++-
 include/linux/dcache.h                             |  50 +++++++-
 kernel/trace/bpf_trace.c                           |   9 +-
 kernel/trace/trace_probe.h                         |   1 +
 kernel/trace/trace_uprobe.c                        |  57 +++++++--
 security/integrity/evm/evm_crypto.c                |   5 +-
 security/integrity/evm/evm_main.c                  |   9 +-
 security/integrity/ima/ima_api.c                   |  17 ++-
 security/integrity/ima/ima_main.c                  |  14 +-
 security/ipe/eval.c                                |  13 +-
 .../selftests/bpf/prog_tests/uprobe_multi_test.c   | 141 +++++++++++++++++++++
 .../testing/selftests/bpf/test_kmods/bpf_testmod.c |  11 +-
 tools/testing/selftests/ftrace/config              |   3 +
 .../test.d/dynevent/add_remove_uprobe_overlayfs.tc |  96 ++++++++++++++
 17 files changed, 423 insertions(+), 52 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261115-work-ovl-d_real-219930df9ec3

author maintainer
#1 lore raw

PATCH v2 1/13 ovl: don't warn when the lazy lowerdata lookup fails in ovl_d_real()

ovl_d_real() looks up the data of a metacopy file in a data-only layer itself if nothing has done that yet. If that lookup fails it WARN()s and returns the overlayfs dentry.

Commit 42dd69ae1af4 ("ovl: implement lazy lookup of lowerdata in data-only layers") assumed that the file would be read before trace_uprobe places a uprobe on it so the data would be there already.

But nothing actually guarantees that. trace_uprobe takes the path from kern_path() and calls d_real_inode() on it when the event is created and again when it is enabled. So a uprobe on a metacopy file whose redirect points at nothing warns twice:

ovl_d_real(/f, 0): real dentry not found

  WARNING: fs/overlayfs/super.c:79 at ovl_d_real+0xc9/0x210, CPU#2: sh/279
  Call Trace:
   <TASK>
   __trace_uprobe_create+0x1869/0x25b0
   trace_probe_create+0xcc/0xe0
   dyn_event_create+0x46/0x70
   create_or_delete_trace_uprobe+0x3c/0x70
   trace_parse_run_command+0x1a6/0x320
   vfs_write+0x1cd/0xfa0
   ksys_write+0x116/0x250
   do_syscall_64+0x10e/0x5e0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>

bpf's uprobe_multi does the same.

A failed lookup isn't a bug though. The data can be missing, its verity digest can be wrong or a signal can interrupt the wait for the inode lock. ovl_open() simply fails in that case and ovl_maybe_lookup_lowerdata() already warns ratelimited about failed lookups. So return the overlayfs dentry without warning. That's what the WARN() path returned anyway.

---
 fs/overlayfs/super.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/overlayfs/super.c +4 -6
index 08695a99f4f93..1985b553a47b7 100644
@@ -58,15 +58,13 @@ static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type)
 
 	/*
 	 * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return
-	 * the real lowerdata dentry.  The only current caller of d_real() with
-	 * D_REAL_DATA is d_real_inode() from trace_uprobe and this caller is
-	 * likely going to be followed reading from the file, before placing
-	 * uprobes on offset within the file, so lowerdata should be available
-	 * when setting the uprobe.
+	 * the real lowerdata dentry. Callers may ask for files that were never
+	 * opened, e.g. for uprobes or O_PATH files, so the lookup can fail
+	 * here. Return the overlayfs dentry then.
 	 */
 	err = ovl_verify_lowerdata(dentry);
 	if (err)
-		goto bug;
+		return dentry;
 	lower = ovl_dentry_lowerdata(dentry);
 	if (!lower)
 		goto bug;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 01/13] ovl: don't warn when the lazy lowerdata lookup fails in ovl_d_real()

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

ovl_d_real() looks up the data of a metacopy file in a data-only layer itself if nothing has done that yet. If that lookup fails it WARN()s and returns the overlayfs dentry.

Commit 42dd69ae1af4 ("ovl: implement lazy lookup of lowerdata in data-only layers") assumed that the file would be read before trace_uprobe places a uprobe on it so the data would be there already.

But nothing actually guarantees that. trace_uprobe takes the path from kern_path() and calls d_real_inode() on it when the event is created and again when it is enabled. So a uprobe on a metacopy file whose redirect points at nothing warns twice:

Quoted 8 lines

ovl_d_real(/f, 0): real dentry not found

  WARNING: fs/overlayfs/super.c:79 at ovl_d_real+0xc9/0x210, CPU#2: sh/279

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/13
#3 lore raw

Re: [PATCH v2 01/13] ovl: don't warn when the lazy lowerdata lookup fails in ovl_d_real()

In reply to Christian Brauner
Quoted 47 lines · Christian Brauner

ovl_d_real() looks up the data of a metacopy file in a data-only layer
itself if nothing has done that yet. If that lookup fails it WARN()s and
returns the overlayfs dentry.

Commit 42dd69ae1af4 ("ovl: implement lazy lookup of lowerdata in data-only layers") assumed that the file would be read before trace_uprobe places a uprobe on it so the data would be there already.

But nothing actually guarantees that. trace_uprobe takes the path from
kern_path() and calls d_real_inode() on it when the event is created and
again when it is enabled. So a uprobe on a metacopy file whose redirect
points at nothing warns twice:

  ovl_d_real(/f, 0): real dentry not found

  WARNING: fs/overlayfs/super.c:79 at ovl_d_real+0xc9/0x210, CPU#2:
sh/279
  Call Trace:
   <TASK>
   __trace_uprobe_create+0x1869/0x25b0
   trace_probe_create+0xcc/0xe0
   dyn_event_create+0x46/0x70
   create_or_delete_trace_uprobe+0x3c/0x70
   trace_parse_run_command+0x1a6/0x320
   vfs_write+0x1cd/0xfa0
   ksys_write+0x116/0x250
   do_syscall_64+0x10e/0x5e0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>

bpf's uprobe_multi does the same.

A failed lookup isn't a bug though. The data can be missing, its verity
digest can be wrong or a signal can interrupt the wait for the inode lock. ovl_open() simply fails in that case and ovl_maybe_lookup_lowerdata() already warns ratelimited about failed lookups. So return the overlayfs dentry without warning. That's what the
WARN() path returned anyway.

Fixes 42dd69ae1af4 ("ovl: implement lazy lookup of lowerdata in
Quoted 8 lines

data-only layers")

Closes:
https://lore.kernel.org/all/[email protected]

Quoted 37 lines

---
 fs/overlayfs/super.c | 10 ++++------

 1 file changed, 4 insertions(+), 6 deletions(-)
fs/overlayfs/super.c +0 -0
index 08695a99f4f93..1985b553a47b7 100644
@@ -58,15 +58,13 @@ static struct dentry *ovl_d_real(struct dentry
 *dentry, enum d_real_type type)


  /*
  * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return

fs/overlayfs/super.c +0 -1
-	 * the real lowerdata dentry.  The only current caller of

d_real() with

fs/overlayfs/super.c +0 -1
-	 * D_REAL_DATA is d_real_inode() from trace_uprobe and this

caller is

fs/overlayfs/super.c +0 -1
-	 * likely going to be followed reading from the file, before

placing

fs/overlayfs/super.c +0 -1
-	 * uprobes on offset within the file, so lowerdata should be

available

fs/overlayfs/super.c +1 -1
-	 * when setting the uprobe.
+	 * the real lowerdata dentry. Callers may ask for files that

were never

fs/overlayfs/super.c +1 -0
+	 * opened, e.g. for uprobes or O_PATH files, so the lookup

can fail

fs/overlayfs/super.c +1 -0
+	 * here. Return the overlayfs dentry then.

  */
  err = ovl_verify_lowerdata(dentry);
  if (err)

fs/overlayfs/super.c +1 -1
-		goto bug;
+		return dentry;

  lower = ovl_dentry_lowerdata(dentry);
  if (!lower)
  goto bug;

author maintainer
#4 lore raw

PATCH v2 2/13 fs: add d_real_data_inode() and d_real_metadata_inode()

In reply to Christian Brauner

Currently, d_real() can't fail. When overlayfs fails to look up the data of a metacopy file in a data-only layer it hands back the overlayfs dentry and callers silently work with the overlayfs inode. That's more problematic than current callers realize.

For trace_uprobe and bpf's uprobe_multi it means uprobe_register() fails with an -EIO that has nothing to do with the actual problem. For IMA it means the stacked change detection is skipped and for IPE the file is evaluated against an inode that has none of the properties of the real file.

Teach d_real() to fail and teach all callers to handle errors. This adds two helpers for the callers to switch over to:

- d_real_data_inode() returns the inode hosting the data or an ERR_PTR() if that can't be resolved.

- d_real_metadata_inode() returns the inode hosting the metadata or an ERR_PTR(). overlayfs never has to look anything up for the metadata but we don't want to promise that it never fails. So callers handle errors for it as well.

d_real() can't fail yet so this is a pure preparation. d_real_inode() stays around until all callers are converted.

No functional changes.

---
 include/linux/dcache.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
include/linux/dcache.h +45 -0
index 84b0cfe4ed0e7..a75016a713d4a 100644
@@ -3,6 +3,7 @@
 #define __LINUX_DCACHE_H
 
 #include <linux/atomic.h>
+#include <linux/err.h>
 #include <linux/list.h>
 #include <linux/math.h>
 #include <linux/rculist.h>
@@ -660,6 +661,50 @@ static inline struct inode *d_real_inode(const struct dentry *dentry)
 	return d_inode(d_real((struct dentry *) dentry, D_REAL_DATA));
 }
 
+/**
+ * d_real_data_inode - Return the real inode hosting the data
+ * @dentry: The dentry to query
+ *
+ * If @dentry is a regular file on a union/overlay, then return the
+ * underlying, real inode that hosts its data. Otherwise return d_inode().
+ *
+ * The union/overlay may have to look up the data first. This can sleep and it
+ * can fail, e.g., because the data is missing or because its verity digest
+ * doesn't match.
+ *
+ * Return: The real inode hosting the data of @dentry, or an ERR_PTR() if it
+ * can't be resolved.
+ */
+static inline struct inode *d_real_data_inode(struct dentry *dentry)
+{
+	struct dentry *real;
+
+	real = d_real(dentry, D_REAL_DATA);
+	if (IS_ERR(real))
+		return ERR_CAST(real);
+	return d_inode(real);
+}
+
+/**
+ * d_real_metadata_inode - Return the real inode hosting the metadata
+ * @dentry: The dentry to query
+ *
+ * If @dentry is a regular file on a union/overlay, then return the
+ * underlying, real inode that hosts its metadata. Otherwise return d_inode().
+ *
+ * Return: The real inode hosting the metadata of @dentry, or an ERR_PTR() if
+ * it can't be resolved.
+ */
+static inline struct inode *d_real_metadata_inode(struct dentry *dentry)
+{
+	struct dentry *real;
+
+	real = d_real(dentry, D_REAL_METADATA);
+	if (IS_ERR(real))
+		return ERR_CAST(real);
+	return d_inode(real);
+}
+
 struct name_snapshot {
 	struct qstr name;
 	union shortname_store inline_name;
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v2 02/13] fs: add d_real_data_inode() and d_real_metadata_inode()

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

Currently, d_real() can't fail. When overlayfs fails to look up the data of a metacopy file in a data-only layer it hands back the overlayfs dentry and callers silently work with the overlayfs inode. That's more problematic than current callers realize.

For trace_uprobe and bpf's uprobe_multi it means uprobe_register() fails with an -EIO that has nothing to do with the actual problem. For IMA it means the stacked change detection is skipped and for IPE the file is evaluated against an inode that has none of the properties of the real file.

Teach d_real() to fail and teach all callers to handle errors. This adds two helpers for the callers to switch over to:

Quoted 7 lines

- d_real_data_inode() returns the inode hosting the data or an ERR_PTR() [ ... ]

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by on v2 2/13
#6 lore raw

Re: [PATCH v2 02/13] fs: add d_real_data_inode() and d_real_metadata_inode()

In reply to Christian Brauner
Quoted 33 lines · Christian Brauner

Currently, d_real() can't fail. When overlayfs fails to look up the data
of a metacopy file in a data-only layer it hands back the overlayfs dentry and callers silently work with the overlayfs inode. That's more
problematic than current callers realize.

For trace_uprobe and bpf's uprobe_multi it means uprobe_register() fails
with an -EIO that has nothing to do with the actual problem. For IMA it
means the stacked change detection is skipped and for IPE the file is evaluated against an inode that has none of the properties of the real
file.

Teach d_real() to fail and teach all callers to handle errors. This adds
two helpers for the callers to switch over to:

- d_real_data_inode() returns the inode hosting the data or an ERR_PTR()
  if that can't be resolved.

- d_real_metadata_inode() returns the inode hosting the metadata or an
  ERR_PTR(). overlayfs never has to look anything up for the metadata but we don't want to promise that it never fails. So callers handle errors for it as well.

d_real() can't fail yet so this is a pure preparation. d_real_inode() stays around until all callers are converted.

Quoted 6 lines

No functional changes.

Link:
https://lore.kernel.org/all/CAOQ4uxhGxpBtMZ5kfuzR1tQAewiecoX7UjXQRo91QjJHH9T_Yw@mail.gmail.com

Quoted 79 lines

---
 include/linux/dcache.h | 45
+++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 45 insertions(+)
include/linux/dcache.h +0 -0
index 84b0cfe4ed0e7..a75016a713d4a 100644
@@ -3,6 +3,7 @@
  #define __LINUX_DCACHE_H


 #include <linux/atomic.h>
+#include <linux/err.h>
 #include <linux/list.h>
 #include <linux/math.h>
 #include <linux/rculist.h>

include/linux/dcache.h +0 -0
@@ -660,6 +661,50 @@ static inline struct inode *d_real_inode(const
 struct dentry *dentry)
  	return d_inode(d_real((struct dentry *) dentry,
 D_REAL_DATA));
  }


+/**
+ * d_real_data_inode - Return the real inode hosting the data
+ * @dentry: The dentry to query
+ *
+ * If @dentry is a regular file on a union/overlay, then return the
+ * underlying, real inode that hosts its data. Otherwise return d_inode().
+ *
+ * The union/overlay may have to look up the data first. This can sleep and it
+ * can fail, e.g., because the data is missing or because its verity digest
+ * doesn't match.
+ *
+ * Return: The real inode hosting the data of @dentry, or an ERR_PTR() if it
+ * can't be resolved.
+ */

include/linux/dcache.h +12 -0
+static inline struct inode *d_real_data_inode(struct dentry *dentry)
+{
+	struct dentry *real;
+
+	real = d_real(dentry, D_REAL_DATA);
+	if (IS_ERR(real))
+		return ERR_CAST(real);
+	return d_inode(real);
+}
+
+/**
+ * d_real_metadata_inode - Return the real inode hosting the

metadata
+ * @dentry: The dentry to query
+ *
+ * If @dentry is a regular file on a union/overlay, then return the
+ * underlying, real inode that hosts its metadata. Otherwise return d_inode().
+ *
+ * Return: The real inode hosting the metadata of @dentry, or an ERR_PTR() if
+ * it can't be resolved.
+ */
+static inline struct inode *d_real_metadata_inode(struct dentry *dentry)

include/linux/dcache.h +9 -0
+{
+	struct dentry *real;
+
+	real = d_real(dentry, D_REAL_METADATA);
+	if (IS_ERR(real))
+		return ERR_CAST(real);
+	return d_inode(real);
+}
+

 struct name_snapshot {
  struct qstr name;
  union shortname_store inline_name;

author maintainer
#7 lore raw

PATCH v2 3/13 tracing/uprobes: handle errors from resolving the real inode

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch trace_uprobe from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, nothing checks whether the data can be resolved when a uprobe is created. The first resolution happens in validate_ref_ctr_offset() under event_mutex and the last one when the event is enabled. If the data of a metacopy file in an overlayfs data-only layer can't be found the event is created just fine and enabling it fails later with an -EIO from uprobe_register() because the overlayfs inode has no ->read_folio().

Instead, resolve the inode hosting the data once in __trace_uprobe_create() right after kern_path(). That happens outside of event_mutex and if it fails the write to uprobe_events or dynamic_events fails with the actual error and a new NO_FILE_DATA entry in the error log. The resolved inode is passed down to validate_ref_ctr_offset() and trace_uprobe_has_same_uprobe() so we don't resolve it again under event_mutex.

Both functions still resolve the existing probes they compare against. If that fails the existing probe can't be on the same inode so we just skip it. For validate_ref_ctr_offset() that's fine because uprobe_register() refuses a mismatching reference counter offset for the same uprobe anyway.

trace_uprobe_enable() returns the error and probe_event_enable() already unwinds the probes that were enabled before.

While at it, resolve the data in create_local_trace_uprobe() as well. perf_event_open() of a uprobe PMU event registers the probe right away so it would fail in trace_uprobe_enable() anyway. But this way the first resolution happens before perf_uprobe_init() takes event_mutex just like for uprobes created through tracefs.

This is a no-op until d_real() can actually fail.

---
 kernel/trace/trace_probe.h  |  1 +
 kernel/trace/trace_uprobe.c | 57 ++++++++++++++++++++++++++++++++++-----------
 2 files changed, 45 insertions(+), 13 deletions(-)
kernel/trace/trace_probe.h +1 -0
index d1fb3520700fb..91bf3a66d834a 100644
@@ -610,6 +610,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
 	C(NO_EVENT_FIELD,	"This event field is not found."),		\
 	C(NO_EVENT_INFO,	"This requires both group and event name to attach"),	\
 	C(NO_EVENT_NAME,	"Event name is not specified"),		\
+	C(NO_FILE_DATA,		"Failed to resolve the data of the given file"),	\
 	C(NO_GROUP_NAME,	"Group name is not specified"),		\
 	C(NO_PTR_STRCT,		"This is not a pointer to union/structure."),	\
 	C(NO_REGULAR_FILE,	"Not a regular file"),		\
kernel/trace/trace_uprobe.c +44 -13

147 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v2 03/13] tracing/uprobes: handle errors from resolving the real inode

Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch trace_uprobe from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, nothing checks whether the data can be resolved when a uprobe is created. The first resolution happens in validate_ref_ctr_offset() under event_mutex and the last one when the event is enabled. If the data of a metacopy file in an overlayfs data-only layer can't be found the event is created just fine and enabling it fails later with an -EIO from uprobe_register() because the overlayfs inode has no ->read_folio().

Instead, resolve the inode hosting the data once in __trace_uprobe_create() right after kern_path(). That happens outside of event_mutex and if it fails the write to uprobe_events or [ ... ]
for uprobes created through tracefs.

Quoted 3 lines

This is a no-op until d_real() can actually fail.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Acked-by on v2 3/13
#9 lore raw

Re: [PATCH v2 03/13] tracing/uprobes: handle errors from resolving the real inode

In reply to Christian Brauner
Quoted 29 lines · Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch trace_uprobe from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, nothing checks whether the data can be resolved when a uprobe is created. The first resolution happens in validate_ref_ctr_offset() under event_mutex and the last one when the event is enabled. If the data of a metacopy file in an overlayfs data-only layer can't be found the event is created just fine and enabling it fails later with an -EIO from uprobe_register() because the overlayfs inode has no ->read_folio().

Instead, resolve the inode hosting the data once in __trace_uprobe_create() right after kern_path(). That happens outside of event_mutex and if it fails the write to uprobe_events or dynamic_events fails with the actual error and a new NO_FILE_DATA entry in the error log. The resolved inode is passed down to validate_ref_ctr_offset() and trace_uprobe_has_same_uprobe() so we don't resolve it again under event_mutex.

Both functions still resolve the existing probes they compare against. If that fails the existing probe can't be on the same inode so we just skip it. For validate_ref_ctr_offset() that's fine because uprobe_register() refuses a mismatching reference counter offset for the same uprobe anyway.

trace_uprobe_enable() returns the error and probe_event_enable() already unwinds the probes that were enabled before.

Quoted 8 lines

While at it, resolve the data in create_local_trace_uprobe() as well. perf_event_open() of a uprobe PMU event registers the probe right away so it would fail in trace_uprobe_enable() anyway. But this way the first resolution happens before perf_uprobe_init() takes event_mutex just like for uprobes created through tracefs.

This is a no-op until d_real() can actually fail.

Looks good to me.

However, since this conflict with commit 72d3a5cb0cd7 ("tracing/probes:
Enable newly appended probe when target event is enabled"), please rebase it on v7.4-rc3.

Quoted 6 lines
---
 kernel/trace/trace_probe.h  |  1 +
 kernel/trace/trace_uprobe.c | 57 ++++++++++++++++++++++++++++++++++-----------
 2 files changed, 45 insertions(+), 13 deletions(-)

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#10 lore raw

Re: [PATCH v2 03/13] tracing/uprobes: handle errors from resolving the real inode

Quoted 39 lines · Masami Hiramatsu (Google)
Quoted 29 lines · Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch trace_uprobe from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, nothing checks whether the data can be resolved when a uprobe is created. The first resolution happens in validate_ref_ctr_offset() under event_mutex and the last one when the event is enabled. If the data of a metacopy file in an overlayfs data-only layer can't be found the event is created just fine and enabling it fails later with an -EIO from uprobe_register() because the overlayfs inode has no ->read_folio().

Instead, resolve the inode hosting the data once in __trace_uprobe_create() right after kern_path(). That happens outside of event_mutex and if it fails the write to uprobe_events or dynamic_events fails with the actual error and a new NO_FILE_DATA entry in the error log. The resolved inode is passed down to validate_ref_ctr_offset() and trace_uprobe_has_same_uprobe() so we don't resolve it again under event_mutex.

Both functions still resolve the existing probes they compare against. If that fails the existing probe can't be on the same inode so we just skip it. For validate_ref_ctr_offset() that's fine because uprobe_register() refuses a mismatching reference counter offset for the same uprobe anyway.

trace_uprobe_enable() returns the error and probe_event_enable() already unwinds the probes that were enabled before.

Quoted 8 lines

While at it, resolve the data in create_local_trace_uprobe() as well. perf_event_open() of a uprobe PMU event registers the probe right away so it would fail in trace_uprobe_enable() anyway. But this way the first resolution happens before perf_uprobe_init() takes event_mutex just like for uprobes created through tracefs.

This is a no-op until d_real() can actually fail.

Quoted 7 lines

Looks good to me.

However, since this conflict with commit 72d3a5cb0cd7 ("tracing/probes:
Enable newly appended probe when target event is enabled"), please rebase it on v7.4-rc3.

Yeah, I had already done that.

author maintainer
#11 lore raw

PATCH v2 4/13 bpf: handle errors from resolving the real inode in uprobe_multi

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch bpf_uprobe_multi_link_attach() from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, the real inode is resolved again for every single uprobe of the link. There's no point in doing that. Resolve it once right after the d_is_reg() check and fail BPF_LINK_CREATE with the error if the data of the file can't be resolved. Then all uprobes of the link are registered on the same inode.

This is a no-op until d_real() can actually fail.

---
 kernel/trace/bpf_trace.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +8 -1
index 2aec56d7f3d3e..15f32b971326e 100644
@@ -3280,6 +3280,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	struct bpf_uprobe *uprobes = NULL;
 	struct task_struct *task = NULL;
 	unsigned long __user *uoffsets;
+	struct inode *inode;
 	u64 __user *ucookies;
 	unsigned long size;
 	u32 flags, cnt, i;
@@ -3336,6 +3337,12 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 		goto error_path_put;
 	}
 
+	inode = d_real_data_inode(path.dentry);
+	if (IS_ERR(inode)) {
+		err = PTR_ERR(inode);
+		goto error_path_put;
+	}
+
 	if (pid) {
 		rcu_read_lock();
 		task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
@@ -3394,7 +3401,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 		      &bpf_uprobe_multi_link_lops, prog, attr->link_create.attach_type);
 
 	for (i = 0; i < cnt; i++) {
-		uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry),
+		uprobes[i].uprobe = uprobe_register(inode,
 						    uprobes[i].offset,
 						    uprobes[i].ref_ctr_offset,
 						    &uprobes[i].consumer);
Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH v2 04/13] bpf: handle errors from resolving the real inode in uprobe_multi

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch bpf_uprobe_multi_link_attach() from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, the real inode is resolved again for every single uprobe of the link. There's no point in doing that. Resolve it once right after the d_is_reg() check and fail BPF_LINK_CREATE with the error if the data of the file can't be resolved. Then all uprobes of the link are registered on the same inode.

Quoted 4 lines

This is a no-op until d_real() can actually fail.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#13 lore raw

PATCH v2 5/13 selftests/bpf: handle d_real_data_inode() errors in bpf_testmod

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch testmod_register_uprobe() from d_real_inode() to d_real_data_inode() and drop the path again if the data of /proc/self/exe can't be resolved.

That's hard to hit in practice because exec opened the file already but it is the only modular user and d_real_inode() is going away.

---
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +9 -2
index 76fbe64755ef9..9d065a800f655 100644
@@ -948,6 +948,7 @@ static struct testmod_uprobe uprobe = {
 
 static __always_inline int testmod_register_uprobe(loff_t offset)
 {
+	struct inode *inode;
 	int err = -EBUSY;
 
 	if (uprobe.uprobe)
@@ -962,8 +963,14 @@ static __always_inline int testmod_register_uprobe(loff_t offset)
 	if (err)
 		goto out;
 
-	uprobe.uprobe = uprobe_register(d_real_inode(uprobe.path.dentry),
-					offset, 0, &uprobe.consumer);
+	inode = d_real_data_inode(uprobe.path.dentry);
+	if (IS_ERR(inode)) {
+		err = PTR_ERR(inode);
+		path_put(&uprobe.path);
+		goto out;
+	}
+
+	uprobe.uprobe = uprobe_register(inode, offset, 0, &uprobe.consumer);
 	if (IS_ERR(uprobe.uprobe)) {
 		err = PTR_ERR(uprobe.uprobe);
 		path_put(&uprobe.path);
Signature · 1 line
2.53.0
#14 lore raw

Re: [PATCH v2 05/13] selftests/bpf: handle d_real_data_inode() errors in bpf_testmod

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch testmod_register_uprobe() from d_real_inode() to d_real_data_inode() and drop the path again if the data of /proc/self/exe can't be resolved.

That's hard to hit in practice because exec opened the file already but it is the only modular user and d_real_inode() is going away.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#15 lore raw

PATCH v2 6/13 ima: handle errors from resolving the real inode

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch IMA from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode. process_measurement() then sees real_inode == inode, skips the detection of changes in the backing file and returns the cached verdict. That's the wrong default. With metacopy and verity this isn't limited to files nobody opened yet either. A metadata-only copy-up of an open file makes overlayfs validate the verity digest of the data again on the next D_REAL_DATA lookup and that can fail.

So if the inode hosting the data can't be resolved in process_measurement() we can't tell whether the data changed. Treat it like a change and clear IMA_DONE_MASK and measured_pcrs so the file is evaluated again. The metadata check for EVM stays independent of that.

This is a no-op until d_real() can actually fail.

---
 security/integrity/ima/ima_api.c  | 17 +++++++++++++----
 security/integrity/ima/ima_main.c |  8 +++++---
 2 files changed, 18 insertions(+), 7 deletions(-)
security/integrity/ima/ima_api.c +13 -4
index 45701f04ba62f..f8c3437559943 100644
@@ -246,7 +246,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
 {
 	const char *audit_cause = "failed";
 	struct inode *inode = file_inode(file);
-	struct inode *real_inode = d_real_inode(file_dentry(file));
+	struct inode *real_inode;
 	struct ima_max_digest_data hash;
 	struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
 						struct ima_digest_data, hdr);
@@ -268,6 +268,13 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
 	if (iint->flags & IMA_COLLECTED)
 		goto out;
 
+	/*
+	 * On stacked filesystems the inode hosting the data may have to be
+	 * looked up and that can fail. Treat it like failing to calculate
+	 * the hash.
+	 */
+	real_inode = d_real_data_inode(file_dentry(file));
+
 	/*
 	 * Detect file change based on STATX_CHANGE_COOKIE, when supported,
 	 * and fallback to detecting file change based on i_version.
@@ -280,7 +287,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
 				   AT_STATX_SYNC_AS_STAT);
 	if (!result && (stat.result_mask & STATX_CHANGE_COOKIE))
 		i_version = stat.change_cookie;
-	else if (IS_I_VERSION(real_inode))
+	else if (!IS_ERR(real_inode) && IS_I_VERSION(real_inode))
 		i_version = inode_peek_iversion(real_inode);
 
 	hash.hdr.algo = algo;
@@ -289,7 +296,9 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
 	/* Initialize hash digest to 0's in case of failure */
 	memset(&hash.digest, 0, sizeof(hash.digest));
 
-	if (iint->flags & IMA_VERITY_REQUIRED) {
+	if (IS_ERR(real_inode)) {
+		result = PTR_ERR(real_inode);
+	} else if (iint->flags & IMA_VERITY_REQUIRED) {
 		if (!ima_get_verity_digest(iint, inode, &hash)) {
 			audit_cause = "no-verity-digest";
 			result = -ENODATA;
@@ -314,7 +323,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
 	memcpy(iint->ima_hash, &hash, length);
 	if (real_inode == inode)
 		iint->real_inode.version = i_version;
-	else
+	else if (!IS_ERR(real_inode))
 		integrity_inode_attrs_store(&iint->real_inode, i_version,
 					    real_inode);
 
security/integrity/ima/ima_main.c +5 -3
index fed80da3ee096..e463cfc1e3f43 100644
@@ -342,12 +342,14 @@ static int process_measurement(struct file *file, const struct cred *cred,
 
 	/*
 	 * On stacked filesystems, detect and re-evaluate file data and
-	 * metadata changes.
+	 * metadata changes. If the inode hosting the data can't be resolved
+	 * we can't tell whether the data changed. Re-evaluate the file then
+	 * and let ima_collect_measurement() fail.
 	 */
-	real_inode = d_real_inode(file_dentry(file));
+	real_inode = d_real_data_inode(file_dentry(file));
 	if (real_inode != inode &&
 	    (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) {
-		if (!IS_I_VERSION(real_inode) ||
+		if (IS_ERR(real_inode) || !IS_I_VERSION(real_inode) ||
 		    integrity_inode_attrs_changed(&iint->real_inode,
 						  real_inode)) {
 			iint->flags &= ~IMA_DONE_MASK;
Signature · 1 line
2.53.0
#16 lore raw

Re: [PATCH v2 06/13] ima: handle errors from resolving the real inode

Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch IMA from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode. process_measurement() then sees real_inode == inode, skips the detection of changes in the backing file and returns the cached verdict. That's the wrong default. With metacopy and verity this isn't limited to files nobody opened yet either. A metadata-only copy-up of an open file makes overlayfs validate the verity digest of the data again on the next D_REAL_DATA lookup and that can fail.

Quoted 8 lines

So if the inode hosting the data can't be resolved in process_measurement() we can't tell whether the data changed. Treat it like a change and clear IMA_DONE_MASK and measured_pcrs so the file is evaluated again. The metadata check for EVM stays independent of that.

This is a no-op until d_real() can actually fail.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer Reviewed-by on v2 6/13
#17 lore raw

Re: [PATCH v2 06/13] ima: handle errors from resolving the real inode

In reply to Christian Brauner
Quoted 12 lines · Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch IMA from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode. process_measurement() then sees real_inode == inode, skips the detection of changes in the backing file and returns the cached verdict. That's the wrong default. With metacopy and verity this isn't limited to files nobody opened yet either. A metadata-only copy-up of an open file makes overlayfs validate the verity digest of the data again on the next D_REAL_DATA lookup and that can fail.

Quoted 8 lines

So if the inode hosting the data can't be resolved in process_measurement() we can't tell whether the data changed. Treat it like a change and clear IMA_DONE_MASK and measured_pcrs so the file is evaluated again. The metadata check for EVM stays independent of that.

This is a no-op until d_real() can actually fail.

The patch, itself, looks fine, but I'm not able to reproduce the error.

The existing IMA overlay tests are working with and without this patch set. The tests check:
* test 1: checks file signature is copied
* test 2: checks backing file signature is copied up to overlay
* test 3: detects modified backing file

Mimi

author maintainer
#18 lore raw

PATCH v2 7/13 ima,evm: use d_real_metadata_inode()

In reply to Christian Brauner

d_real() is about to be able to fail. D_REAL_METADATA never has to look anything up but we don't want to promise that it never fails. So switch the open-coded d_inode(d_real(..., D_REAL_METADATA)) in process_measurement() and evm_calc_hmac_or_hash() over to d_real_metadata_inode() and deal with errors.

evm_calc_hmac_or_hash() returns the error so EVM fails to verify the metadata. process_measurement() passes the error on to evm_metadata_changed() which treats it like changed metadata and resets the EVM status. That is needed as evm_verify_hmac() would otherwise hand out a cached INTEGRITY_PASS without looking at the metadata again.

This is a no-op as long as overlayfs can't fail D_REAL_METADATA.

---
 security/integrity/evm/evm_crypto.c | 5 ++++-
 security/integrity/evm/evm_main.c   | 9 +++++----
 security/integrity/ima/ima_main.c   | 6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
security/integrity/evm/evm_crypto.c +4 -1
index 1c41af2f91a60..17d528771d8fe 100644
@@ -231,7 +231,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 				 uint8_t type, struct evm_digest *data,
 				 struct evm_iint_cache *iint)
 {
-	struct inode *inode = d_inode(d_real(dentry, D_REAL_METADATA));
+	struct inode *inode = d_real_metadata_inode(dentry);
 	struct xattr_list *xattr;
 	struct shash_desc *desc;
 	size_t xattr_size = 0;
@@ -241,6 +241,9 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 	bool ima_present = false;
 	u64 i_version = 0;
 
+	if (IS_ERR(inode))
+		return PTR_ERR(inode);
+
 	if (!(inode->i_opflags & IOP_XATTR) ||
 	    inode->i_sb->s_user_ns != &init_user_ns)
 		return -EOPNOTSUPP;
security/integrity/evm/evm_main.c +5 -4
index ac8328812e6ef..71faf156f9d6b 100644
@@ -763,11 +763,11 @@ static void evm_reset_status(struct inode *inode)
 /**
  * evm_metadata_changed: Detect changes to the metadata
  * @inode: a file's inode
- * @metadata_inode: metadata inode
+ * @metadata_inode: metadata inode, or an ERR_PTR() if it can't be resolved
  *
  * On a stacked filesystem detect whether the metadata has changed. If this is
- * the case reset the evm_status associated with the inode that represents the
- * file.
+ * the case, or if the metadata inode can't be resolved, reset the evm_status
+ * associated with the inode that represents the file.
  */
 bool evm_metadata_changed(struct inode *inode, struct inode *metadata_inode)
 {
@@ -775,7 +775,8 @@ bool evm_metadata_changed(struct inode *inode, struct inode *metadata_inode)
 	bool ret = false;
 
 	if (iint) {
-		ret = (!IS_I_VERSION(metadata_inode) ||
+		ret = (IS_ERR(metadata_inode) ||
+		       !IS_I_VERSION(metadata_inode) ||
 		       integrity_inode_attrs_changed(&iint->metadata_inode,
 						     metadata_inode));
 		if (ret)
security/integrity/ima/ima_main.c +3 -3
index e463cfc1e3f43..016de5a66eced 100644
@@ -357,10 +357,10 @@ static int process_measurement(struct file *file, const struct cred *cred,
 		}
 
 		/*
-		 * Reset the EVM status when metadata changed.
+		 * Reset the EVM status when metadata changed or the inode
+		 * hosting it can't be resolved.
 		 */
-		metadata_inode = d_inode(d_real(file_dentry(file),
-					 D_REAL_METADATA));
+		metadata_inode = d_real_metadata_inode(file_dentry(file));
 		if (evm_metadata_changed(inode, metadata_inode))
 			iint->flags &= ~(IMA_APPRAISED |
 					 IMA_APPRAISED_SUBMASK);
Signature · 1 line
2.53.0
#19 lore raw

Re: [PATCH v2 07/13] ima,evm: use d_real_metadata_inode()

Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

d_real() is about to be able to fail. D_REAL_METADATA never has to look anything up but we don't want to promise that it never fails. So switch the open-coded d_inode(d_real(..., D_REAL_METADATA)) in process_measurement() and evm_calc_hmac_or_hash() over to d_real_metadata_inode() and deal with errors.

evm_calc_hmac_or_hash() returns the error so EVM fails to verify the metadata. process_measurement() passes the error on to evm_metadata_changed() which treats it like changed metadata and resets the EVM status. That is needed as evm_verify_hmac() would otherwise hand out a cached INTEGRITY_PASS without looking at the metadata again.

Quoted 5 lines

This is a no-op as long as overlayfs can't fail D_REAL_METADATA.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Reviewed-by on v2 7/13
#20 lore raw

Re: [PATCH v2 07/13] ima,evm: use d_real_metadata_inode()

In reply to Christian Brauner
Quoted 16 lines · Christian Brauner

d_real() is about to be able to fail. D_REAL_METADATA never has to look
anything up but we don't want to promise that it never fails. So switch
the open-coded d_inode(d_real(..., D_REAL_METADATA)) in process_measurement() and evm_calc_hmac_or_hash() over to d_real_metadata_inode() and deal with errors.

evm_calc_hmac_or_hash() returns the error so EVM fails to verify the metadata. process_measurement() passes the error on to evm_metadata_changed() which treats it like changed metadata and resets
the EVM status. That is needed as evm_verify_hmac() would otherwise hand
out a cached INTEGRITY_PASS without looking at the metadata again.

Quoted 5 lines

This is a no-op as long as overlayfs can't fail D_REAL_METADATA.

Link:
https://lore.kernel.org/all/CAOQ4uxjoHbevvu8auB4m8AVcY9H6uHdDAP6mfi9OOMh+3775xA@mail.gmail.com

Quoted 96 lines

---
 security/integrity/evm/evm_crypto.c | 5 ++++- security/integrity/evm/evm_main.c   | 9 +++++---- security/integrity/ima/ima_main.c   | 6 +++---

 3 files changed, 12 insertions(+), 8 deletions(-)
security/integrity/evm/evm_crypto.c +0 -0
index 1c41af2f91a60..17d528771d8fe 100644
@@ -231,7 +231,7 @@ static int evm_calc_hmac_or_hash(struct dentry
 *dentry,

  uint8_t type, struct evm_digest *data,
  struct evm_iint_cache *iint) {

security/integrity/evm/evm_crypto.c +0 -1
-	struct inode *inode = d_inode(d_real(dentry,

D_REAL_METADATA));

security/integrity/evm/evm_crypto.c +1 -0
+	struct inode *inode = d_real_metadata_inode(dentry);

  struct xattr_list *xattr;
  struct shash_desc *desc;
  size_t xattr_size = 0;

security/integrity/evm/evm_crypto.c +0 -0
@@ -241,6 +241,9 @@ static int evm_calc_hmac_or_hash(struct dentry
 *dentry,
  	bool ima_present = false;
  	u64 i_version = 0;

security/integrity/evm/evm_crypto.c +3 -0
+	if (IS_ERR(inode))
+		return PTR_ERR(inode);
+

  if (!(inode->i_opflags & IOP_XATTR) || inode->i_sb->s_user_ns != &init_user_ns) return -EOPNOTSUPP;

security/integrity/evm/evm_main.c +1 -1
index ac8328812e6ef..71faf156f9d6b 100644
@@ -763,11 +763,11 @@ static void evm_reset_status(struct inode
 *inode)
  /**
   * evm_metadata_changed: Detect changes to the metadata
   * @inode: a file's inode
- * @metadata_inode: metadata inode
+ * @metadata_inode: metadata inode, or an ERR_PTR() if it can't be

resolved
  *
  * On a stacked filesystem detect whether the metadata has changed. If this is
- * the case reset the evm_status associated with the inode that represents the
- * file.
+ * the case, or if the metadata inode can't be resolved, reset the evm_status
+ * associated with the inode that represents the file. */
 bool evm_metadata_changed(struct inode *inode, struct inode *metadata_inode)
 {

security/integrity/evm/evm_main.c +0 -0
@@ -775,7 +775,8 @@ bool evm_metadata_changed(struct inode *inode,
 struct inode *metadata_inode)
  	bool ret = false;


  if (iint) {

security/integrity/evm/evm_main.c +2 -1
-		ret = (!IS_I_VERSION(metadata_inode) ||
+		ret = (IS_ERR(metadata_inode) ||
+		       !IS_I_VERSION(metadata_inode) ||

         integrity_inode_attrs_changed(&iint-

Quoted 1 line

metadata_inode,


metadata_inode));
  if (ret)

security/integrity/ima/ima_main.c +0 -0
index e463cfc1e3f43..016de5a66eced 100644
@@ -357,10 +357,10 @@ static int process_measurement(struct file
 *file, const struct cred *cred,
  		}


  /*

security/integrity/ima/ima_main.c +1 -1
-		 * Reset the EVM status when metadata changed.
+		 * Reset the EVM status when metadata changed or the

inode

security/integrity/ima/ima_main.c +1 -0
+		 * hosting it can't be resolved.

  */

security/integrity/ima/ima_main.c +1 -2
-		metadata_inode = d_inode(d_real(file_dentry(file),
-					 D_REAL_METADATA));
+		metadata_inode =

d_real_metadata_inode(file_dentry(file));
  if (evm_metadata_changed(inode, metadata_inode)) iint->flags &= ~(IMA_APPRAISED | IMA_APPRAISED_SUBMASK);

author maintainer
#21 lore raw

PATCH v2 8/13 ipe: handle errors from resolving the real inode

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch ipe_build_eval_ctx() from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode and IPE evaluates the policy against that. The overlayfs inode has neither a block device nor any fs-verity state so none of the properties of the file hosting the data are visible.

Instead, leave the block device and inode state of the evaluation context unset if the data can't be resolved so the file is evaluated without any dm-verity or fs-verity state. IOW, IPE reaches the same verdict as today but without looking at the overlayfs inode.

This is a no-op until d_real() can actually fail.

---
 security/ipe/eval.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
security/ipe/eval.c +10 -3
index 4a7be96c84f03..a2366eb9a47b3 100644
@@ -102,9 +102,16 @@ void ipe_build_eval_ctx(struct ipe_eval_ctx *ctx,
 
 	if (file) {
 		build_ipe_sb_ctx(ctx, file);
-		ino = d_real_inode(file->f_path.dentry);
-		build_ipe_bdev_ctx(ctx, ino);
-		build_ipe_inode_ctx(ctx, ino);
+		/*
+		 * If the real data inode cannot be resolved, leave the bdev
+		 * and inode fields unset so the file is evaluated without
+		 * dm-verity/fs-verity state.
+		 */
+		ino = d_real_data_inode(file->f_path.dentry);
+		if (!IS_ERR(ino)) {
+			build_ipe_bdev_ctx(ctx, ino);
+			build_ipe_inode_ctx(ctx, ino);
+		}
 	}
 }
 
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH v2 08/13] ipe: handle errors from resolving the real inode

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch ipe_build_eval_ctx() from d_real_inode() to d_real_data_inode() and deal with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode and IPE evaluates the policy against that. The overlayfs inode has neither a block device nor any fs-verity state so none of the properties of the file hosting the data are visible.

Instead, leave the block device and inode state of the evaluation context unset if the data can't be resolved so the file is evaluated without any dm-verity or fs-verity state. IOW, IPE reaches the same verdict as today but without looking at the overlayfs inode.

Quoted 6 lines

This is a no-op until d_real() can actually fail.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Reviewed-by on v2 8/13
#23 lore raw

Re: [PATCH v2 08/13] ipe: handle errors from resolving the real inode

In reply to Christian Brauner
Quoted 16 lines · Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch ipe_build_eval_ctx() from d_real_inode() to d_real_data_inode() and deal
with the error.

Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode and IPE evaluates the policy against that. The
overlayfs inode has neither a block device nor any fs-verity state so none of the properties of the file hosting the data are visible.

Instead, leave the block device and inode state of the evaluation context unset if the data can't be resolved so the file is evaluated without any dm-verity or fs-verity state. IOW, IPE reaches the same verdict as today but without looking at the overlayfs inode.

Quoted 6 lines

This is a no-op until d_real() can actually fail.

Link:
https://lore.kernel.org/all/[email protected]

Quoted 32 lines

---
 security/ipe/eval.c | 13 ++++++++++---

 1 file changed, 10 insertions(+), 3 deletions(-)
security/ipe/eval.c +0 -0
index 4a7be96c84f03..a2366eb9a47b3 100644
@@ -102,9 +102,16 @@ void ipe_build_eval_ctx(struct ipe_eval_ctx
 *ctx,


  if (file) {
  build_ipe_sb_ctx(ctx, file);

security/ipe/eval.c +2 -3
-		ino = d_real_inode(file->f_path.dentry);
-		build_ipe_bdev_ctx(ctx, ino);
-		build_ipe_inode_ctx(ctx, ino);
+		/*
+		 * If the real data inode cannot be resolved, leave

the bdev

security/ipe/eval.c +1 -0
+		 * and inode fields unset so the file is evaluated

without

security/ipe/eval.c +7 -0
+		 * dm-verity/fs-verity state.
+		 */
+		ino = d_real_data_inode(file->f_path.dentry);
+		if (!IS_ERR(ino)) {
+			build_ipe_bdev_ctx(ctx, ino);
+			build_ipe_inode_ctx(ctx, ino);
+		}

  }
 }

author maintainer
#24 lore raw

PATCH v2 9/13 bpf: return NULL from bpf_real_data_inode() on errors

In reply to Christian Brauner

d_real() is about to be able to fail for D_REAL_DATA. So switch bpf_real_data_inode() from d_real_inode() to d_real_data_inode() and return NULL if the data can't be resolved.

The kfunc is already KF_SLEEPABLE | KF_RET_NULL and its documentation says it may return NULL. So programs already have to deal with NULL and the verifier enforces that. Note that this is the caller that most easily ends up with a file whose data was never looked up. lsm.s/file_open runs before ovl_open() and lsm.s/file_receive sees O_PATH files passed via SCM_RIGHTS.

Spell that out in the documentation.

This is a no-op until d_real() can actually fail.

---
 fs/bpf_fs_kfuncs.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/bpf_fs_kfuncs.c +11 -2
index 35ccd98a4de64..ab24e6bcd64c4 100644
@@ -491,11 +491,20 @@ __bpf_kfunc int bpf_sock_read_xattr(struct socket *sock, const char *name__str,
  * inode itself is returned; for any file on a non-union filesystem the inode
  * attached to @file is returned.
  *
- * Return: The inode hosting @file's data, or NULL.
+ * The union/overlay may have to look up the data first, e.g., when @file was
+ * opened with O_PATH or the program runs before the file is opened. If that
+ * fails NULL is returned.
+ *
+ * Return: The inode hosting @file's data, or NULL if it can't be resolved.
  */
 __bpf_kfunc struct inode *bpf_real_data_inode(struct file *file)
 {
-	return d_real_inode(file_dentry(file));
+	struct inode *inode;
+
+	inode = d_real_data_inode(file_dentry(file));
+	if (IS_ERR(inode))
+		return NULL;
+	return inode;
 }
 
 __bpf_kfunc_end_defs();
Signature · 1 line
2.53.0
#25 lore raw

Re: [PATCH v2 09/13] bpf: return NULL from bpf_real_data_inode() on errors

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

d_real() is about to be able to fail for D_REAL_DATA. So switch bpf_real_data_inode() from d_real_inode() to d_real_data_inode() and return NULL if the data can't be resolved.

The kfunc is already KF_SLEEPABLE | KF_RET_NULL and its documentation says it may return NULL. So programs already have to deal with NULL and the verifier enforces that. Note that this is the caller that most easily ends up with a file whose data was never looked up. lsm.s/file_open runs before ovl_open() and lsm.s/file_receive sees O_PATH files passed via SCM_RIGHTS.

Quoted 6 lines

Spell that out in the documentation.

This is a no-op until d_real() can actually fail.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author maintainer
#26 lore raw

PATCH v2 10/13 ovl: return the error from the lazy lowerdata lookup in ovl_d_real()

In reply to Christian Brauner

All callers have been converted to d_real_data_inode() and handle an error now. So return the error from ovl_verify_lowerdata() just like ovl_open() does. The lowerdata dentry missing after a successful ovl_verify_lowerdata() would be an overlayfs bug, so keep warning about that but return -EIO like ovl_open() does when it has no real path. D_REAL_METADATA never looks anything up and still can't fail.

Update the comment in ovl_d_real() and the documentation of d_real() in dcache.h and vfs.rst accordingly.

---
 Documentation/filesystems/vfs.rst |  7 +++++++
 fs/overlayfs/super.c              | 17 +++++++++--------
 include/linux/dcache.h            |  6 ++++++
 3 files changed, 22 insertions(+), 8 deletions(-)
Documentation/filesystems/vfs.rst +7 -0
index 1824462a43711..ff7ea00a42ad7 100644
@@ -1374,6 +1374,13 @@ defined:
 	for returning the real underlying dentry that refers to the inode
 	hosting the file's data or metadata respectively.
 
+	For D_REAL_DATA the filesystem may have to look up the data first,
+	e.g., for a file that was never opened.  If that fails an ERR_PTR()
+	is returned and callers must handle it.  Callers must handle an
+	ERR_PTR() for D_REAL_METADATA as well.  Most callers should use
+	d_real_data_inode() or d_real_metadata_inode() instead of calling
+	d_real() directly.
+
 	For non-regular files, the 'dentry' argument is returned.
 
 ``d_unalias_trylock``
fs/overlayfs/super.c +9 -8
index 1985b553a47b7..39d4d57dec230 100644
@@ -42,7 +42,7 @@ static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type)
 	}
 
 	if (!d_is_reg(dentry)) {
-		/* d_real_inode() is only relevant for regular files */
+		/* d_real() is only relevant for regular files */
 		return dentry;
 	}
 
@@ -57,17 +57,18 @@ static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type)
 	}
 
 	/*
-	 * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return
-	 * the real lowerdata dentry. Callers may ask for files that were never
-	 * opened, e.g. for uprobes or O_PATH files, so the lookup can fail
-	 * here. Return the overlayfs dentry then.
+	 * Lazy lookup of lowerdata for the D_REAL_DATA case to return the real
+	 * lowerdata dentry. Callers may ask for files that were never opened,
+	 * e.g., for uprobes or O_PATH files, and the lookup or the verity
+	 * validation can fail. Return the error then, just like ovl_open()
+	 * would. Callers of d_real() have to deal with that.
 	 */
 	err = ovl_verify_lowerdata(dentry);
 	if (err)
-		return dentry;
+		return ERR_PTR(err);
 	lower = ovl_dentry_lowerdata(dentry);
-	if (!lower)
-		goto bug;
+	if (WARN_ON_ONCE(!lower))
+		return ERR_PTR(-EIO);
 
 real_lower:
 	/* Handle recursion into stacked lower fs */
include/linux/dcache.h +6 -0
index a75016a713d4a..fd8853622f250 100644
@@ -638,7 +638,13 @@ static inline struct inode *d_backing_inode(const struct dentry *upper)
  * If dentry is on a union/overlay, then return the underlying, real dentry.
  * Otherwise return the dentry itself.
  *
+ * For D_REAL_DATA the union/overlay may have to look up the data first. This
+ * can sleep and it can fail. Callers must handle errors for D_REAL_METADATA
+ * as well.
+ *
  * See also: Documentation/filesystems/vfs.rst
+ *
+ * Return: The real dentry, or an ERR_PTR() if it can't be resolved.
  */
 static inline struct dentry *d_real(struct dentry *dentry, enum d_real_type type)
 {
Signature · 1 line
2.53.0
#27 lore raw

Re: [PATCH v2 10/13] ovl: return the error from the lazy lowerdata lookup in ovl_d_real()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

All callers have been converted to d_real_data_inode() and handle an error now. So return the error from ovl_verify_lowerdata() just like ovl_open() does. The lowerdata dentry missing after a successful ovl_verify_lowerdata() would be an overlayfs bug, so keep warning about that but return -EIO like ovl_open() does when it has no real path. D_REAL_METADATA never looks anything up and still can't fail.

Quoted 7 lines

Update the comment in ovl_d_real() and the documentation of d_real() in dcache.h and vfs.rst accordingly.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author maintainer
#28 lore raw

PATCH v2 11/13 fs: remove d_real_inode()

In reply to Christian Brauner

All callers have been converted to d_real_data_inode() or d_real_metadata_inode(). Remove d_real_inode() so that any user we missed, or one that gets added in another tree, fails to build instead of passing an ERR_PTR() from d_real() to d_inode().

Add a porting.rst entry.

---
 Documentation/filesystems/porting.rst | 10 ++++++++++
 include/linux/dcache.h                | 13 -------------
 2 files changed, 10 insertions(+), 13 deletions(-)
Documentation/filesystems/porting.rst +10 -0
index c38f5df755bb5..aa0fc43907017 100644
@@ -1422,3 +1422,13 @@ helpers take a const struct mnt_idmap *. mnt_idmap() and file_mnt_idmap()
 return one. The idmapping is immutable so nothing should have modified it
 anyway. References are taken and dropped via mnt_idmap_get() and
 mnt_idmap_put() as before, both accept a const pointer.
+
+---
+
+**mandatory**
+
+d_real_inode() is gone. d_real() can fail for D_REAL_DATA now and returns an
+ERR_PTR() then, e.g., when overlayfs can't look up the data of a metacopy file
+in a data-only layer. Use d_real_data_inode(), which returns the inode hosting
+the data or an ERR_PTR(), and handle the error. Use d_real_metadata_inode() for
+the inode hosting the metadata and handle its errors as well.
include/linux/dcache.h +0 -13
index fd8853622f250..fe1aa8fea0c7a 100644
@@ -654,19 +654,6 @@ static inline struct dentry *d_real(struct dentry *dentry, enum d_real_type type
 		return dentry;
 }
 
-/**
- * d_real_inode - Return the real inode hosting the data
- * @dentry: The dentry to query
- *
- * If dentry is on a union/overlay, then return the underlying, real inode.
- * Otherwise return d_inode().
- */
-static inline struct inode *d_real_inode(const struct dentry *dentry)
-{
-	/* This usage of d_real() results in const dentry */
-	return d_inode(d_real((struct dentry *) dentry, D_REAL_DATA));
-}
-
 /**
  * d_real_data_inode - Return the real inode hosting the data
  * @dentry: The dentry to query
Signature · 1 line
2.53.0
#29 lore raw

Re: [PATCH v2 11/13] fs: remove d_real_inode()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

All callers have been converted to d_real_data_inode() or d_real_metadata_inode(). Remove d_real_inode() so that any user we missed, or one that gets added in another tree, fails to build instead of passing an ERR_PTR() from d_real() to d_inode().

Add a porting.rst entry.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Reviewed-by on v2 11/13
#30 lore raw

Re: [PATCH v2 11/13] fs: remove d_real_inode()

In reply to Christian Brauner
Quoted 9 lines · Christian Brauner

All callers have been converted to d_real_data_inode() or d_real_metadata_inode(). Remove d_real_inode() so that any user we missed, or one that gets added in another tree, fails to build instead
of passing an ERR_PTR() from d_real() to d_inode().

Add a porting.rst entry.

Quoted 59 lines

---
 Documentation/filesystems/porting.rst | 10 ++++++++++ include/linux/dcache.h                | 13 -------------

 2 files changed, 10 insertions(+), 13 deletions(-)
Documentation/filesystems/porting.rst +0 -0
index c38f5df755bb5..aa0fc43907017 100644
@@ -1422,3 +1422,13 @@ helpers take a const struct mnt_idmap *.
 mnt_idmap() and file_mnt_idmap()

 return one. The idmapping is immutable so nothing should have modified it
 anyway. References are taken and dropped via mnt_idmap_get() and mnt_idmap_put() as before, both accept a const pointer.

Documentation/filesystems/porting.rst +6 -0
+
+---
+
+**mandatory**
+
+d_real_inode() is gone. d_real() can fail for D_REAL_DATA now and

returns an
+ERR_PTR() then, e.g., when overlayfs can't look up the data of a metacopy file
+in a data-only layer. Use d_real_data_inode(), which returns the inode hosting
+the data or an ERR_PTR(), and handle the error. Use d_real_metadata_inode() for

Documentation/filesystems/porting.rst +1 -0
+the inode hosting the metadata and handle its errors as well.
include/linux/dcache.h +0 -0
index fd8853622f250..fe1aa8fea0c7a 100644
@@ -654,19 +654,6 @@ static inline struct dentry *d_real(struct

dentry *dentry, enum d_real_type type
  return dentry;
 }

-/**
- * d_real_inode - Return the real inode hosting the data
- * @dentry: The dentry to query
- *
- * If dentry is on a union/overlay, then return the underlying, real inode.
- * Otherwise return d_inode().
- */
-static inline struct inode *d_real_inode(const struct dentry *dentry)

include/linux/dcache.h +0 -3
-{
-	/* This usage of d_real() results in const dentry */
-	return d_inode(d_real((struct dentry *) dentry,

D_REAL_DATA));

include/linux/dcache.h +0 -2
-}
-

 /**
  * d_real_data_inode - Return the real inode hosting the data
  * @dentry: The dentry to query

author maintainer
#31 lore raw

PATCH v2 12/13 selftests/ftrace: add uprobe tests on overlayfs

In reply to Christian Brauner

Add uprobe tests on overlayfs with a data-only layer:

- a metacopy file whose redirect points at nothing: creating the probe fails, the error log has the new message and nothing gets tainted by a WARN

- a metacopy file whose data lives in the data-only layer: the data is looked up when the probe is created and the probe hits when the file is executed

None of the existing tests puts a uprobe on overlayfs.

---
 tools/testing/selftests/ftrace/config              |  3 +
 .../test.d/dynevent/add_remove_uprobe_overlayfs.tc | 96 ++++++++++++++++++++++
 2 files changed, 99 insertions(+)
tools/testing/selftests/ftrace/config +3 -0
index 29f05f7c6e2ce..05325737e4a5f 100644
@@ -19,6 +19,7 @@ CONFIG_KPROBE_EVENTS=y
 CONFIG_MAGIC_SYSRQ=y
 CONFIG_MODULES=y
 CONFIG_MODULE_UNLOAD=y
+CONFIG_OVERLAY_FS=y
 CONFIG_PREEMPTIRQ_DELAY_TEST=m
 CONFIG_PREEMPT_TRACER=y
 CONFIG_PROBE_EVENTS_BTF_ARGS=y
@@ -30,6 +31,8 @@ CONFIG_SAMPLE_TRACE_PRINTK=m
 CONFIG_SCHED_TRACER=y
 CONFIG_STACK_TRACER=y
 CONFIG_SYNTH_EVENTS=y
+CONFIG_TMPFS=y
+CONFIG_TMPFS_XATTR=y
 CONFIG_TRACER_SNAPSHOT=y
 CONFIG_UPROBES=y
 CONFIG_UPROBE_EVENTS=y
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.53.0
#32 lore raw

Re: [PATCH v2 12/13] selftests/ftrace: add uprobe tests on overlayfs

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Add uprobe tests on overlayfs with a data-only layer:

- a metacopy file whose redirect points at nothing: creating the probe fails, the error log has the new message and nothing gets tainted by a WARN

Quoted 7 lines

- a metacopy file whose data lives in the data-only layer: the data is looked up when the probe is created and the probe hits when the file is executed

None of the existing tests puts a uprobe on overlayfs.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#33 lore raw

Re: [PATCH v2 12/13] selftests/ftrace: add uprobe tests on overlayfs

In reply to Christian Brauner

tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc line 4

Quoted 8 lines · Christian Brauner
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc +4 -0
@@ -0,0 +1,96 @@
+
+OVL=$TMPDIR/ovl
+mkdir $OVL
+mount -t tmpfs tmpfs $OVL

nit: If CONFIG_TMPFS is not enabled in the running kernel, mount -t tmpfs will fail under set -e and report [FAIL] instead of [UNSUPPORTED].

Maybe you need to check tmpfs support in /proc/filesystems too?

tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc line 45

Quoted 8 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc +7 -0
+
+cleanup() {
+  echo 0 > events/enable
[...]
+setfattr -n trusted.overlay.metacopy $OVL/lower/broken
+setfattr -n trusted.overlay.redirect -v /missing $OVL/lower/broken
+
+mount -t overlay overlay -o lowerdir=$OVL/lower::$OVL/data $OVL/merged

Similarly to test_attach_api_fails_ovl_no_data() in patch 13/13, if mounting overlayfs with data-only lower layers (lowerdir=$OVL/lower::$OVL/data) fails (e.g., on an older kernel that lacks data-only layer support), it would be better to skip with exit_unsupported rather than failing the test.

(Note that this selftest can be ran on older stable kernels.)

Thank you,

Quoted 3 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_uprobe_overlayfs.tc +3 -0
+
+TAINT_WARN=512
+TAINTED=`cat /proc/sys/kernel/tainted`
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#34 lore raw

PATCH v2 13/13 selftests/bpf: add uprobe_multi test on overlayfs without data

In reply to Christian Brauner

Add a uprobe_multi test that attaches to a metacopy file on overlayfs whose redirect into a data-only layer points at nothing:

- the overlayfs is set up in a child in its own mount namespace

- BPF_LINK_CREATE must fail with ENOENT from the lazy lowerdata lookup

- skip if overlayfs with data-only layers isn't available

Before d_real() could fail this got an EIO from uprobe_register().

---
 .../selftests/bpf/prog_tests/uprobe_multi_test.c   | 141 +++++++++++++++++++++
 1 file changed, 141 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +141 -0
index 7fbcfddc3cc36..907d8c6d5ec93 100644
@@ -3,6 +3,10 @@
 #include <unistd.h>
 #include <pthread.h>
 #include <fcntl.h>
+#include <sched.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
 #include <test_progs.h>
 #include "uprobe_multi.skel.h"
 #include "uprobe_multi_bench.skel.h"
@@ -575,6 +579,141 @@ static void test_attach_api_fails(void)
 	uprobe_multi__destroy(skel);
 }
 
+enum ovl_no_data_stage {
+	OVL_NO_DATA_SETUP,
+	OVL_NO_DATA_MOUNT,
+	OVL_NO_DATA_ATTACH,
+};
+
+struct ovl_no_data_result {
+	enum ovl_no_data_stage stage;
+	int err;
+};
+
+/*
+ * Runs in a child in its own mount namespace. Create an overlayfs with a
+ * metacopy file whose redirect into a data-only layer points at nothing
+ * and try to attach to it.
+ */
+static void ovl_no_data_child(int prog_fd, const char *dir, int wfd)
+{
+	struct ovl_no_data_result res = { .stage = OVL_NO_DATA_SETUP };
+	LIBBPF_OPTS(bpf_link_create_opts, opts);
+	char path[PATH_MAX], data[PATH_MAX * 2 + 32];
+	unsigned long offset = 0;
+	int fd;
+
+	if (unshare(CLONE_NEWNS) ||
+	    mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) ||
+	    mount("tmpfs", dir, "tmpfs", 0, NULL))
+		goto out_errno;
+
+	snprintf(path, sizeof(path), "%s/lower", dir);
+	if (mkdir(path, 0755))
+		goto out_errno;
+	snprintf(path, sizeof(path), "%s/data", dir);
+	if (mkdir(path, 0755))
+		goto out_errno;
+	snprintf(path, sizeof(path), "%s/merged", dir);
+	if (mkdir(path, 0755))
+		goto out_errno;
+
+	snprintf(path, sizeof(path), "%s/lower/f", dir);
+	fd = open(path, O_CREAT | O_WRONLY, 0755);
+	if (fd < 0)
+		goto out_errno;
+	close(fd);
+	if (setxattr(path, "trusted.overlay.metacopy", "", 0, 0) ||
+	    setxattr(path, "trusted.overlay.redirect", "/missing",
+		     strlen("/missing"), 0))
+		goto out_errno;
+
+	res.stage = OVL_NO_DATA_MOUNT;
+	snprintf(data, sizeof(data), "lowerdir=%s/lower::%s/data", dir, dir);
+	snprintf(path, sizeof(path), "%s/merged", dir);
+	if (mount("overlay", path, "overlay", 0, data))
+		goto out_errno;
+
+	res.stage = OVL_NO_DATA_ATTACH;
+	snprintf(path, sizeof(path), "%s/merged/f", dir);
+	opts.uprobe_multi.path = path;
+	opts.uprobe_multi.offsets = &offset;
+	opts.uprobe_multi.cnt = 1;
+	res.err = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &opts);
+	if (res.err >= 0)
+		close(res.err);
+	goto out;
+
+out_errno:
+	res.err = -errno;
+out:
+	if (write(wfd, &res, sizeof(res)) != sizeof(res))
+		_exit(1);
+	_exit(0);
+}
+
+/*
+ * The data of a metacopy file in an overlayfs data-only layer is looked up
+ * lazily. If it can't be found attaching must fail with the error from the
+ * lookup.
+ */
+static void test_attach_api_fails_ovl_no_data(void)
+{
+	struct ovl_no_data_result res = {};
+	char dir[] = "/tmp/uprobe_multi_ovl.XXXXXX";
+	struct uprobe_multi *skel = NULL;
+	int pipefd[2] = { -1, -1 };
+	int status, prog_fd;
+	ssize_t n;
+	pid_t pid;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
+		return;
+	prog_fd = bpf_program__fd(skel->progs.uprobe_extra);
+
+	if (!ASSERT_OK_PTR(mkdtemp(dir), "mkdtemp"))
+		goto cleanup;
+	if (!ASSERT_OK(pipe(pipefd), "pipe"))
+		goto cleanup_dir;
+
+	pid = fork();
+	if (!ASSERT_GE(pid, 0, "fork"))
+		goto cleanup_pipe;
+	if (pid == 0) {
+		close(pipefd[0]);
+		ovl_no_data_child(prog_fd, dir, pipefd[1]);
+	}
+
+	close(pipefd[1]);
+	pipefd[1] = -1;
+	n = read(pipefd[0], &res, sizeof(res));
+	if (!ASSERT_EQ(waitpid(pid, &status, 0), pid, "waitpid"))
+		goto cleanup_pipe;
+	if (!ASSERT_EQ(n, sizeof(res), "read result"))
+		goto cleanup_pipe;
+
+	if (res.stage == OVL_NO_DATA_MOUNT) {
+		printf("%s:SKIP: overlayfs with data-only layers not available (%d)\n",
+		       __func__, res.err);
+		test__skip();
+		goto cleanup_pipe;
+	}
+	if (!ASSERT_EQ(res.stage, OVL_NO_DATA_ATTACH, "setup"))
+		goto cleanup_pipe;
+	ASSERT_EQ(res.err, -ENOENT, "link_create");
+
+cleanup_pipe:
+	if (pipefd[0] >= 0)
+		close(pipefd[0]);
+	if (pipefd[1] >= 0)
+		close(pipefd[1]);
+cleanup_dir:
+	rmdir(dir);
+cleanup:
+	uprobe_multi__destroy(skel);
+}
+
 #ifdef __x86_64__
 noinline void uprobe_multi_error_func(void)
 {
@@ -1452,6 +1591,8 @@ void test_uprobe_multi_test(void)
 		test_bench_attach_usdt();
 	if (test__start_subtest("attach_api_fails"))
 		test_attach_api_fails();
+	if (test__start_subtest("attach_api_fails_ovl_no_data"))
+		test_attach_api_fails_ovl_no_data();
 	if (test__start_subtest("attach_uprobe_fails"))
 		test_attach_uprobe_fails();
 	if (test__start_subtest("consumers"))
Signature · 1 line
2.53.0
#35 lore raw

Re: [PATCH v2 13/13] selftests/bpf: add uprobe_multi test on overlayfs without data

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

Add a uprobe_multi test that attaches to a metacopy file on overlayfs whose redirect into a data-only layer points at nothing:

- the overlayfs is set up in a child in its own mount namespace

Quoted 8 lines

- BPF_LINK_CREATE must fail with ENOENT from the lazy lowerdata lookup

- skip if overlayfs with data-only layers isn't available

Before d_real() could fail this got an EIO from uprobe_register().

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13