Skip to contentSkip to conversation

package.bbclass: better handling of directory symlinks

PATCH New Markus Lehtonen 1 participant 3 messages Aug 4, 2016

lore
Share & tools
lore
#0 lore raw

cover package.bbclass: better handling of directory symlinks

FILES specifications that list files under directory symlinks can be problematic in packaging. Packaging fails if you specify a file within a symlinked directory but the real target directory is not included. The behavior on files like this is currently non-deterministic, even for valid configurations. Packaging may succeed or fail, depending on the order that files are specified in FILES. This patchset addresses the situation.

The first patch checks FILES for specs that point to files that reside under symlinked directories. It then handles these separately, failing gracefully if packaging is not possible (because the real target directory is not included in the pkg). The second patch simply adds a warning if it finds FILE specifications that point to files undert symlinked directories.

Basically, these problematic FILE specifications should not be used at all - files should be specified by their real path. It would probably be best to just fail if such constructs are found. However, making such a change might have unwanted consecuences by breaking existing builds. The first patch is not particularly beautiful but it should make handling of symlinks deterministic without breaking anything. Printing a warning hopefully courages to fix potentially problematic recipes. At some point in the future the first patch could be essentially reverted and the warning turned into an error.

[YOCTO #9827]

The following changes since commit 986f7fbe37a48d050611f08f7160ed96755ac3dc:

foomatic-filters: Security fixes CVE-2015-8327 (2016-08-01 12:11:09 +0100)

are available in the git repository at:

git://git.openembedded.org/openembedded-core-contrib marquiz/fixes-9827
http://git.openembedded.org/openembedded-core-contrib/log/?h=marquiz/fixes-9827

Markus Lehtonen (2):
package.bbclass: better handling of middle-path dir symlinks package.bbclass: warn about files under symlinked directories

 meta/classes/package.bbclass | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
Signature · 1 line
2.6.6
#1 lore raw

PATCH 1/2 package.bbclass: better handling of middle-path dir symlinks

For example in a directory structure like this

    .
    ├── symlink -> foo/bar
    └── foo
        └── bar
            └── file

'file' could be referenced by specifying e.g. 'foo/bar/file' or 'symlink/file'. In cases like this populate_packages() might crash if the file was referenced (in FILES) via the symlinked directory. The outcome depends on how the user defined FILES_pn. This patch should make the function behave more consistently. It looks for files which are referenced via symlinked directories and handles them separately, failing if their parent directory is a non-existent path. For example, defining FILES_{PN} = "symlink/file" causes a build failure because symlinks target 'foo/bar' is not included at all.

[YOCTO #9827]

---
 meta/classes/package.bbclass | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
meta/classes/package.bbclass +26 -4
index d6204cf..df010e1 100644
@@ -259,14 +259,27 @@ def files_from_filevars(filevars):
                 continue
         files.append(f)
 
-    for f in files:
+    symlink_paths = []
+    for ind, f in enumerate(files):
+        # Handle directory symlinks. Truncate path to the lowest level symlink
+        parent = ''
+        for dirname in f.split('/')[:-1]:
+            parent = os.path.join(parent, dirname)
+            if dirname == '.':
+                continue
+            if cpath.islink(parent):
+                symlink_paths.append(f)
+                files[ind] = parent
+                f = parent
+                break
+
         if not cpath.islink(f):
             if cpath.isdir(f):
                 newfiles = [ os.path.join(f,x) for x in os.listdir(f) ]
                 if newfiles:
                     files += newfiles
 
-    return files
+    return files, symlink_paths
 
 # Called in package_<rpm,ipk,deb>.bbclass to get the correct list of configuration files
 def get_conffiles(pkg, d):
@@ -281,7 +294,7 @@ def get_conffiles(pkg, d):
     if conffiles == None:
         conffiles = ""
     conffiles = conffiles.split()
-    conf_orig_list = files_from_filevars(conffiles)
+    conf_orig_list = files_from_filevars(conffiles)[0]
 
     # Remove links and directories from conf_orig_list to get conf_list which only contains normal files
     conf_list = []
@@ -1111,7 +1124,7 @@ python populate_packages () {
             filesvar.replace("//", "/")
 
         origfiles = filesvar.split()
-        files = files_from_filevars(origfiles)
+        files, symlink_paths = files_from_filevars(origfiles)
 
         if autodebug and pkg.endswith("-dbg"):
             files.extend(debug)
@@ -1160,6 +1173,15 @@ python populate_packages () {
             if ret is False or ret == 0:
                 raise bb.build.FuncFailed("File population failed")
 
+        # Check if symlink paths exist
+        for file in symlink_paths:
+            if not os.path.exists(os.path.join(root,file)):
+                bb.fatal("File '%s' cannot be packaged into '%s' because its "
+                         "parent directory structure does not exist. One of "
+                         "its parent directories is a symlink whose target "
+                         "directory is not included in the package." %
+                         (file, pkg))
+
     os.umask(oldumask)
     os.chdir(workdir)
 
Signature · 1 line
2.6.6
#2 lore raw

PATCH 2/2 package.bbclass: warn about files under symlinked directories

In reply to Markus Lehtonen

[YOCTO #9827]

---
 meta/classes/package.bbclass | 3 +++
 1 file changed, 3 insertions(+)
meta/classes/package.bbclass +3 -0
index df010e1..a9709ad 100644
@@ -268,6 +268,9 @@ def files_from_filevars(filevars):
             if dirname == '.':
                 continue
             if cpath.islink(parent):
+                bb.warn("FILES contains file '%s' which resides under a "
+                        "directory symlink. Please fix the recipe and use the "
+                        "real path for the file." % f[1:])
                 symlink_paths.append(f)
                 files[ind] = parent
                 f = parent
Signature · 1 line
2.6.6