Debian Codegraph @

docs/source/design-decisions.rst

raw
1 Design Decisions
2 ================
3
4 The goal of this page is to explicitly document and give reasoning for design decisions that were made in this project.
5
6 "We don't guess" Philosophy
7 ---------------------------
8
9 The philosophy for ``debsbom`` when accruing information from a distribution is simple: "We don't guess". That means that ``debsbom`` always prefers providing less information to providing unverified information. In other words: if anything in the SBOM is wrong, it is a bug.
10
11 Offline SBOM Generation
12 -----------------------
13
14 ``debsbom`` generates SBOMs without accessing any resource from the internet. It is intended to be used for airgapped build environments, where Internet access is not possible. This also ensures the SBOM generation is reproducible.
15
16 What this also means is that some information that would theoretically be available by a simple lookup on e.g. snapshot.debian.org is not available in the generated SBOM.
17
18 This restriction does not go for other commands, e.g. the ``download`` command requires Internet access of course.
19
20 Information Sources
21 -------------------
22
23 ``debsbom`` only uses data available from ``dpkg`` and ``apt``. It does not rely on the command line utilities, but instead directly parses its internal data. When using the ``generate`` command the following files are accessed:
24
25 .. csv-table::
26 :header: File, Reason
27 :widths: 15, 15
28
29 /var/lib/dpkg/status, Contains the installed package list for the distribution; is required unless ``--from-pkglist`` is used
30 /var/lib/dpkg/arch-native, Contains the native architecture for the distribution when written by dpkg 1.22.16 or newer; optional
31 /var/lib/apt/lists/*, "Contains apt-cache information, used for enrichment of source and binary packages; optional"
32 /var/lib/apt/extended-states, Contains information which packages are manually installed; used for building of the dependency graph; optional
33 /usr/share/doc/*/copyright, Contains licensing information for installed packages; only used with the ``--with-licenses`` option; optional
34
35 Mapping of Debian Binary Packages to SBOM Packages/Components
36 -------------------------------------------------------------
37
38 The following table shows how fields in a Debian binary package are mapped to fields in a SBOM component or package.
39
40 .. csv-table:: Debian Binary Packages to SBOM Mappings
41 :header: Debian Package Field, SPDX Package Field, CDX Component Field
42 :widths: 15, 15, 15
43
44 ``Package`` [#always_there]_, ``name``, ``name``
45 ``Status``, [#status]_, [#status]_
46 ``Priority``, [#priority]_, [#priority]_
47 ``Essential``, [#essential]_, [#essential]_
48 ``Section``, \-, ``properties``
49 ``Installed-Size``, \-, \-
50 ``Maintainer``, ``supplier``, ``supplier``
51 ``Architecture`` [#always_there]_, [#architecture]_, [#architecture]_
52 ``Multi-Arch``, \-, \-
53 ``Source``, [#source]_, [#source]_
54 ``Version`` [#always_there]_, ``versionInfo``, ``version``
55 ``Breaks``, \-, \-
56 ``Replaces``, \-, \-
57 ``Provides``, [#provides]_, [#provides]_
58 ``Conflicts``, \-, \-
59 ``Conffiles``, \-, \-
60 ``Depends``, [#depends]_, [#depends]_
61 ``Recommends``, [#recommends]_, [#recommends]_
62 ``Pre-Depends``, [#pre_depends]_, [#pre_depends]_
63 ``Suggests``, [#suggests]_, [#suggests]_
64 ``Description``, ``summary`` and ``description`` [#description]_, ``description``
65 ``Built-Using``, [#built_using_spdx]_, [#built_using_cdx]_
66 ``Static-Built-Using``, [#built_using_spdx]_, [#built_using_cdx]_
67 ``Homepage``, ``homepage``, ``externalReferences.type`` = ``website``
68 ``Description-md5``, \-, \-
69 ``SHA256``, ``checksums``, ``hashes``
70 ``Size``, \-, \-
71 ``Filename``, \-, \-
72
73 .. [#status] If the status is not ``install ok installed`` it is not placed in the SBOM
74 .. [#priority] Packages with ``Priority: required`` are placed directly under the root node in the dependency graph
75 .. [#essential] Essential Packages are placed directly under the root node in the dependency graph
76 .. [#architecture] The architecture is only part of the PURL
77 .. [#source] When a ``Source`` is specified it gets a separate entry in the SBOM and the dependency is added
78 .. [#provides] Provided packages are considered in the dependency resolution
79 .. [#depends] When a ``Dependency`` is specified a dependency is created for the related packages
80 .. [#recommends] Recommended packages are treated as normal dependencies, see `Relationships of Recommended and Suggested Packages`_ for details
81 .. [#suggests] Suggested packages are treated as normal dependencies, see `Relationships of Recommended and Suggested Packages`_ for details
82 .. [#pre_depends] ``Pre-Depends`` dependencies are treated exactly the same as ``Depends`` dependencies, i.e. simply appended to them
83 .. [#description] The synopsis (first line) is the ``summary``, the rest goes into the ``description``
84 .. [#built_using_spdx] Any ``Built-Using`` or ``Static-Built-Using`` dependency gets a separate entry and the ``GENERATED_FROM`` relationship is used
85 .. [#built_using_cdx] Any ``Built-Using`` or ``Static-Built-Using`` dependency gets a separate entry and the dependency is added
86
87 Mapping of Debian Source Packages to SBOM Packages/Components
88 -------------------------------------------------------------
89
90 In the same fashion, this table shows how fields in a Debian source package are mapped to fields in a SBOM component or package.
91
92 .. csv-table:: Debian Source Packages to SBOM Mappings
93 :header: "Debian Package Field", "SPDX Package Field", "CDX Component Field"
94 :widths: 15, 15, 15
95
96 ``Package`` [#always_there]_, ``name``, ``name``
97 ``Binary``, \-, \-
98 ``Version`` [#always_there]_, ``versionInfo``, ``version``
99 ``Maintainer``, ``supplier``, ``supplier``
100 ``Uploaders``, \-, \-
101 ``Build-Depends``, [#build_depends_source]_, [#build_depends_source]_
102 ``Build-Depends-Indep``, \-, \-
103 ``Architecture``, \-, \-
104 ``Standards-Version``, \-, \-
105 ``Format``, \-, \-
106 ``Files``, \-, \-
107 ``Vcs-Browser``, \-, \-
108 ``Vcs-<type>``, ``externalRefs.referenceType`` = ``vcs``, ``externalReferences.type`` = ``vcs``
109 ``Checksums-Sha256``, ``checksums``, ``hashes``
110 ``Homepage``, ``homepage``, ``externalReferences.type`` = ``website``
111 ``Package-List``, \-, \-
112 ``Directory``, \-, \-
113 ``Priority``, \-, \-
114 ``Section``, \-, ``properties``
115
116 .. [#build_depends_source] Correctly resolving the ``Build-Depends`` requires downloading the ``.buildinfo`` for a package, which is not possible offline
117 .. [#always_there] This field is always present in the SBOM
118
119 Relationships between Binary Packages, Source Packages and ``Built-Using``
120 --------------------------------------------------------------------------
121
122 A binary package can have three types of relationships: a binary dependency that points simply to another binary package, a source dependency that points to a source package, and a ``Built-Using`` relationship that also points to a source package. For the exact meaning of these relationships please refer to the `Debian documentation <https://www.debian.org/doc/debian-policy/ch-relationships.html#>`__.
123
124 In CylconeDX SBOMs all relationships are only expressed as the same dependencies. There is no way to differentiate a source dependency and a ``Built-Using`` dependency.
125
126 For SPDX refer to the following table:
127
128 .. csv-table::
129 :header: "Relationship", "SPDX"
130 :widths: 15, 15
131
132 Binary Dependency, ``relationshipType = DEPENDS_ON``
133 Source Dependency, ``relationshipType = GENERATES``
134 ``Built-Using``, ``relationshipType = "GENERATED FROM`` and ``comment = built-using`` [#built_using_relationship]_
135
136 .. [#built_using_relationship] The subject and object of the relationship are reversed compared to the source dependency
137
138 Relationships of Recommended and Suggested Packages
139 ---------------------------------------------------
140
141 Additional to proper dependencies binary packages can also specify recommended and suggested packages. As packages that are recommended or suggested by other packages are not automatically removed by ``apt autoremove`` they can show up as disconnected isles in the dependency graph. To prevent this an edge between the package that recommended/suggested them is required. Unfortunately ``apt`` does not allow us to exactly reconstruct who actually pulled the package in, so these dependencies might not always be fully accurate.
142
143 The generation of these edges in the graph is controlled by the ``--recommends-deps`` and ``--suggests-deps`` flags for the ``generate`` command. By default ``debsbom`` only considers recommended package-dependencies as ``apt`` does install recommended packages in the default configuration. The relationship itself is modeled as a binary dependency. For SPDX SBOMs an additional comment is added: ``recommends`` or ``suggests`` for the respective relationship type.
144
145 Classification of Components/Packages
146 -------------------------------------
147
148 For CycloneDX SBOMs each component has a ``type`` associated with it. We would like to differentiate between a source and binary package with this, but unfortunately there is not yet a good way to do that. There is `ongoing work <https://github.com/CycloneDX/specification/issues/612>`__, which would make it possibly to do that by introducing a new ``classifier`` field for components. As it stands that new field is not going to be included in the spec in the foreseeable future, so the only way to know what kind of package type a component is describing is by looking at the PURL and see if there is a qualifier ``arch=source``.
149
150 For SPDX SBOMs it is possible to differentiate them: we use ``primaryPackagePurpose = LIBRARY`` for binary packages and ``primaryPackagePurpose = SOURCE`` for source packages.
151
152 Checksums
153 ---------
154
155 ``debsbom`` includes checksums in components/packages. For binary packages these refer to the checksum of the ``.deb`` file.
156
157 The situation is more complex for source packages, as these consist of multiple files and there is no checksum given for the collection of them. ``debsbom`` uses the checksum of the ``.dsc`` file in each source package, as this file describes the whole source package and its contents, together with checksums for all other included files. This makes it possible to verify that every file in a source package is as expected.
158
159 When there are multiple checksums of different algorithms available ``debsbom`` checks them in a specific internal order. The first algorithm that is supported is checked. The result of this check is final. The order from highest to lowest is ``SHA256``, ``SHA1``, ``MD5``.
160
161 Uniqueness of a Debian Package
162 ------------------------------
163
164 What makes a Debian package unique is surprisingly complicated. For binary packages the triplet of package name, version and architecture should be enough to uniquely identify a package, but a package can both be found under that same triplet on different archives: the normal ``debian`` archive and ``debian-ports``.
165
166 For source packages the situation should also be simple: a tuple of package name and version is enough. Unfortunately `there are situations where that is also not the case <https://lists.debian.org/debian-snapshot/2025/10/threads.html#00000>`__.
167
168 ``debsbom`` generally uses the simple assumption that package name, version and architecture (for binary packages) uniquely describes the package. If a checksum is available, this checksum is always verified to make sure this assumption actually holds. Where no checksum is available there is no way to be certain that the package is correctly identified.
169
170 There is a unique case for source packages: even if the checksum does not match it could still be content-equal, as is the case with the above example on the mailing list. There, only the signature date changed, which leads to the different checksums. In theory one could verify the content of a source package, as all checksums referenced in the ``.dsc`` would still be the same, but that is not done for the sake of simplicity.
171
172 There is also the problem of package identification. This is relevant for the ``merge`` command, as there components or packages from two different SBOMs need to be merged, if they are identical. The identification of packages, as we have just learned, is complicated and ``debsbom`` simply uses the PURL for identification purposes there, additionally checksums are checked if they are available. This is consistent with the general approach of package identification, as the PURL contains the package name, version and architecture.
173
174 Distribution Architecture
175 -------------------------
176
177 One could assume that the knowledge of the actual architecture of the distribution is not required when generating an SBOM, as the binary packages contain their architecture and we should be able to resolve everything from there. Unfortunately this is not the case and we need to know for which architecture the SBOM is generated.
178
179 The reason for that is the dependency resolution for packages which have the ``all`` architecture. Take for example the ``lmodern`` package:
180
181 .. code-block::
182
183 Package: lmodern
184 Status: install ok installed
185 Priority: optional
186 Section: fonts
187 Installed-Size: 33268
188 Maintainer: Debian TeX Task Force <[email protected]>
189 Architecture: all
190 Multi-Arch: foreign
191 Version: 2.005-1
192 Replaces: lm, lmodern-x11
193 Depends: tex-common (>= 6.13), xfonts-utils, fonts-lmodern (= 2.005-1)
194 [...]
195
196 We can see that the ``Depends`` line does not specify which architecture the dependencies have. The Debian policy in these cases is that both native and ``all`` architecture are possible. The ``xfonts-utils`` dependency could thus resolve to multiple packages. In this case we would need to resolve to the native architecture version of the package, and for that we need to know what the architecture actually is.
197
198 When scanning a rootfs, ``debsbom`` resolves the distribution architecture in this order:
199
200 #. The value specified with ``--distro-arch``.
201 #. The value in ``/var/lib/dpkg/arch-native``.
202 #. The ``Architecture`` of the installed ``dpkg`` package in ``/var/lib/dpkg/status``.
203
204 The ``dpkg`` post-installation script writes ``DPKG_MAINTSCRIPT_ARCH`` to ``arch-native``. This is the same architecture recorded for the installed ``dpkg`` package, so deriving it from the status file does not introduce a guess. If the ``dpkg`` package is absent, generation fails and ``--distro-arch`` must be specified.
205
206 Status-file derivation applies only when scanning a rootfs. Package data passed with ``--from-pkglist`` is assumed to be unrelated to the rootfs and is not used for this purpose. ``debsbom`` also does not use ``/var/lib/dpkg/arch``, which can list both native and foreign architectures, inspect architecture-specific filesystem paths, or execute the rootfs's ``dpkg`` command.
207
208 Universal Ingress and Apt-Cache
209 -------------------------------
210
211 The universal ingress, often seen with the ``--from-pkglist`` option, has some caveats when dealing with enrichment from the apt-cache. A naive assumption might be that these two commands produce the same output:
212
213 .. code-block::
214
215 debsbom generate -r /my-root
216
217 cat /my-root/var/lib/dpkg/status | debsbom generate -r /my-root --from-pkglist
218
219 In fact, there are subtle differences: in the first case the information from the ``extended_states`` file is used to find out which packages are manually installed and which are automatically pulled in as dependencies. In the second command all packages in the status file are considered manually installed, which would affect the dependency graph.
220
221 That is because we assume that with the ``--from-pkglist`` option the rootfs and the packages are not directly related. We can still find packages from the universal ingress in the apt-cache and enrich their entries, but any other local information does not apply to them. In general you should be careful when using universal ingress, as depending on the rootfs you are using and the state of the apt-cache in there, you might have reproducibility issues.
222
223 License Information
224 -------------------
225
226 ``debsbom`` provides license information when SBOM generation is run with the ``--with-licenses`` option. The license information is taken from the Debian copyright files under ``/usr/share/doc/**/copyright``. We aggregate all license found in these copyright files and try to convert them to `valid SPDX license expressions <https://spdx.org/licenses/>`__. All found license expressions are then combined with ``AND``. The approach is very conservative, that means if we fail to identify any license or expression in the copyright file we simply do not emit any license information in the SBOM.
227
228 As the specifiers in Debian copyright files are not SPDX identifiers a conversion table based on the `syntax specification <https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/#license-specification>`__ is used.
229
230 Debian license exception names are free-form, so complete license names containing exceptions are mapped only when both the license and exception have been verified. These mappings are case-insensitive, as required by the Debian copyright format, but ``debsbom`` does not normalize separators or partially or fuzzily match exception names.
231
232 For SPDX SBOMs the information is placed in the `declared licenses field <https://spdx.github.io/spdx-spec/v2.3/package-information/#715-declared-license-field>`__; and for CDX SBOMs in the `licenses field <https://cyclonedx.org/docs/1.6/json/#components_items_licenses>`__ with the ``acknowledgement`` set to ``declared``. This is done this way because ``debsbom`` does not perform any analysis of included licenses and just trusts what is stated by the package authors. This can be expressed in the SBOMs with the above approach. For CDX it could alternatively be placed in the `evidence.licenses field <https://cyclonedx.org/docs/1.6/json/#components_items_evidence_licenses>`__ as there is, as far as we can tell, no clear distinction between the meaning of the two if all ``acknowledgments`` are set to ``declared``. We decided to put it directly in ``licenses``, as that is the first place a user would expect it.
233
234 Limitations
235 ^^^^^^^^^^^
236
237 Annotation of license information is currently only possible for ~50% of all packages. As ``debsbom`` is not a license scanner it relies solely on the information provided by the package authors. This information is unfortunately sometimes not machine-readable and sometimes includes custom or esoteric licenses, ambiguous exception names, or ``public domain`` licensing. Nonetheless, if you find any unrecognized license specifiers that can be cleanly mapped to SPDX identifiers please let us know and we will try to incorporate them.
238
239 If you want to add more complete license information the SBOM you can use a proper scanner and add that information to the SBOM manually. If enough interest arises we could also consider adding plugin infrastructure to directly incorporate different scanners during the SBOM generation.