Skip to contentSkip to conversation

kernel_include.py: fix a regression on linux-next

PATCH New Mauro Carvalho Chehab 1 participant 3 messages Sep 1, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover kernel_include.py: fix a regression on linux-next

Hi Jon,

My past series broke when O= is used. This small series fix it.

Actually, only the first patch is needed. The second one is a cleanup. I opted to split on two just because it contains an incompatible behavior change. Not really a problem, as only media uses kernel-include, but still I opted to keep it in separate, just in case someone might be needing it.

Mauro Carvalho Chehab (2):
docs: kernel_include.py: fix an issue when O= is used
docs: kernel_include.py: drop some old behavior

 Documentation/sphinx/kernel_include.py | 43 ++++++++++++++++++++------
 1 file changed, 33 insertions(+), 10 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 docs: kernel_include.py: fix an issue when O= is used

As reported by Stephen, building docs with O= is now broken. Fix it by ensuring that it will seek files under Kernel source tree.

The original logic was defined to accept including files under Documentation/output. The new logic doesn't need it anymore for media, but it might still be useful to preserve the previous behavior. So, I ended preserving it.

---
 Documentation/sphinx/kernel_include.py | 44 +++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)
Documentation/sphinx/kernel_include.py +39 -5
index 23566ab74866..661ed978bed8 100755
@@ -314,15 +314,49 @@ class KernelInclude(Directive):
     def run(self):
         """Include a file as part of the content of this reST file."""
         env = self.state.document.settings.env
-        path = os.path.realpath(os.path.expandvars(self.arguments[0]))
 
-        # to get a bit security back, prohibit /etc:
-        if path.startswith(os.sep + "etc"):
-            raise self.severe('Problems with "%s" directive, prohibited path: %s' %
-                              (self.name, path))
+        #
+        # The include logic accepts only patches relative to:
+        #   - Kernel source tree
+        #   - Documentation output directory
+        #
+        # The logic does check it to prevent directory traverse
+        #
+
+        srctree = os.path.abspath(os.environ["srctree"])
+
+        path = os.path.expandvars(self.arguments[0])
+        src_path = os.path.join(srctree, path)
+
+        if os.path.isfile(src_path):
+            base = srctree
+            path = src_path
+        elif os.path.exists(arg):
+            # Allow patches from output dir
+            base = os.getcwd()
+            path = os.path.abspath(path)
+        else:
+            raise self.warning(f'File "%s" doesn\'t exist', path)
+
+        abs_base = os.path.abspath(base)
+        abs_full_path = os.path.abspath(os.path.join(base, path))
+
+        try:
+            if os.path.commonpath([abs_full_path, abs_base]) != abs_base:
+                raise self.severe('Problems with "%s" directive, prohibited path: %s' %
+                                  (self.name, path))
+        except ValueError:
+            # Paths don't have the same drive (Windows) or other incompatibility
+            raise self.severe('Problems with "%s" directive, invalid path: %s' %
+                            (self.name, path))
 
         self.arguments[0] = path
 
+        #
+        # Add path location to Sphinx dependencies to ensure proper cache
+        # invalidation check.
+        #
+
         env.note_dependency(os.path.abspath(path))
 
         # HINT: I had to copy&paste the whole Include.run method. I'am not happy
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/2 docs: kernel_include.py: drop some old behavior

In reply to Mauro Carvalho Chehab

The old behavior is not using anymore, so let's drop it.

---
 Documentation/sphinx/kernel_include.py | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
Documentation/sphinx/kernel_include.py +3 -14
index 661ed978bed8..2c4bb8215b4c 100755
@@ -316,11 +316,9 @@ class KernelInclude(Directive):
         env = self.state.document.settings.env
 
         #
-        # The include logic accepts only patches relative to:
-        #   - Kernel source tree
-        #   - Documentation output directory
-        #
-        # The logic does check it to prevent directory traverse
+        # The include logic accepts only patches relative to the
+        # Kernel source tree.  The logic does check it to prevent
+        # directory traverse issues.
         #
 
         srctree = os.path.abspath(os.environ["srctree"])
@@ -331,10 +329,6 @@ class KernelInclude(Directive):
         if os.path.isfile(src_path):
             base = srctree
             path = src_path
-        elif os.path.exists(arg):
-            # Allow patches from output dir
-            base = os.getcwd()
-            path = os.path.abspath(path)
         else:
             raise self.warning(f'File "%s" doesn\'t exist', path)
 
@@ -359,11 +353,6 @@ class KernelInclude(Directive):
 
         env.note_dependency(os.path.abspath(path))
 
-        # HINT: I had to copy&paste the whole Include.run method. I'am not happy
-        # with this, but due to security reasons, the Include.run method does
-        # not allow absolute or relative pathnames pointing to locations *above*
-        # the filesystem tree where the reST document is placed.
-
         if not self.state.document.settings.file_insertion_enabled:
             raise self.warning('"%s" directive disabled.' % self.name)
         source = self.state_machine.input_lines.source(self.lineno -
Signature · 1 line
2.51.0