pax_global_header00006660000000000000000000000064151011114530014502gustar00rootroot0000000000000052 comment=9e93e212d24eeedf1d61bfd8d9864940a4834b6e latchset-luksmeta-9e93e21/000077500000000000000000000000001510111145300154775ustar00rootroot00000000000000latchset-luksmeta-9e93e21/.gitignore000066400000000000000000000004401510111145300174650ustar00rootroot00000000000000*~ *.a *.o *.la *.lo *.log *.m4 *.so *.swp *.swo *.trs .* aclocal.m4 ar-lib autom4te.cache build compile config.guess config.log config.status config.sub configure configure-stamp depcomp install-sh libtool ltmain.sh luksmeta luksmeta.pc Makefile.in Makefile missing tags test-* vgcore.* latchset-luksmeta-9e93e21/.travis.yml000066400000000000000000000027261510111145300176170ustar00rootroot00000000000000dist: trusty sudo: required arch: - amd64 - ppc64le language: c before_script: - autoreconf -if script: - CFLAGS=-coverage LDFLAGS=-lgcov ./configure --prefix=/usr - make -k check V=1 TESTS= - if ! sudo make check; then cat ./test-suite.log ; exit 1; fi compiler: - clang - gcc matrix: exclude: - arch: ppc64le compiler: clang addons: apt: packages: - libcryptsetup-dev coverity_scan: project: name: "latchset/luksmeta" description: "LUKSMeta builds via Travis CI" notification_email: npmccallum@redhat.com build_command_prepend: "./configure; make clean" build_command: "make -j 4" branch_pattern: coverity_scan env: global: - secure: "ehbWCKZW2ObWNXo4w1XvrrKdChVEL/qHQwZNsLVzuI8z/aVau3tF1BLFgxoDJmyO8iVM1nLDEbZ7NChVpggqpYVXX/Jq7zo1S77q6mXvv6MPFVfokukgsnXrkxUJtJVE0rs47Xu8lDT454pKjTaG2zbaEuBNOaMmohVWWIwAxGcNAEtUGIxir0chma1x5wmpXLNQ3UO6+SoqAh0OD9hPFxEaMD+3eNCemmgkrTknqduOU8hntdskT05u0uH50TLSxMWhOwDcqkub2zc2g0E4WWtFXyqdJS6mQv6nYO5l2JiM0rIKzGA7ST2dyKmN/iV3lT+g/PsMhzRE19NT3gqXlWjLEu7ufCoLRqJkthTwfdKfB1o5uwPttin89ZTtkRgthWkVwRtKh+4jL7FWsbip1RcMJURBs1A0e6/AX1EQ9UmdrKLddKwrBy9j2hVX65Fq6rmznZ5QLPJgZKYtUC8qA7L39C0PyJQThoeqSa8JpozSl+l4YSC8c/5E8s2MuiLdX1kuSyFXqgZPGbTt0TLlMsG7FINnvmzYwSO24EsUqmjTz+FfOHpGu/3Jpt23bjZ0iunSB0pQ3jgqnJ9HZRq+4BzqesqXtD9mcfEAwHcPTsj0kRGEoptDT+MlZZ9Dpi/lFghnglIbkpyL95u6t21s8Q9OQ0GhiZpEuMnYimxklMo=" after_success: - bash <(curl -s https://codecov.io/bash) latchset-luksmeta-9e93e21/COPYING000066400000000000000000000636761510111145300165540ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU Lesser General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU Lesser General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU Lesser General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU Lesser General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! latchset-luksmeta-9e93e21/Makefile.am000066400000000000000000000027341510111145300175410ustar00rootroot00000000000000AM_CFLAGS = @LUKSMETA_CFLAGS@ @cryptsetup_CFLAGS@ BUILT_SOURCES= CLEANFILES= noinst_LTLIBRARIES = libcrc32c.la libcrc32c_la_SOURCES = crc32c.c crc32c.h include_HEADERS = luksmeta.h lib_LTLIBRARIES = libluksmeta.la libluksmeta_la_LDFLAGS = -export-symbols-regex '^luksmeta_' libluksmeta_la_LIBADD = libcrc32c.la @cryptsetup_LIBS@ bin_PROGRAMS = luksmeta luksmeta_LDADD = libluksmeta.la @cryptsetup_LIBS@ man_ADOC_FILES = luksmeta.8.adoc if HAVE_A2X man_ROFF_FILES = $(man_ADOC_FILES:.adoc=.roff) BUILT_SOURCES += $(man_ROFF_FILES) CLEANFILES += $(man_ROFF_FILES) $(man_ROFF_FILES:.roff=) $(top_builddir)/%.roff: %.adoc $(MKDIR_P) $$(dirname $@) $(A2X) -v -f manpage $^ -D $(top_builddir)/$$(dirname $@) $(INSTALL) -m 644 $(top_builddir)/$(@:.roff=) $(top_builddir)/$@ man8_MANS = luksmeta.8 else man8_MANS = endif pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = luksmeta.pc check_LTLIBRARIES = libtest.la libtest_la_SOURCES = test.c test.h check_PROGRAMS = test-crc32c test-lm-assumptions test-lm-init test-lm-one test-lm-two test-lm-big test_crc32c_LDADD = libcrc32c.la test_lm_assumptions_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@ test_lm_init_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@ test_lm_one_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@ test_lm_two_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@ test_lm_big_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@ EXTRA_DIST = $(man_ADOC_FILES) test-luksmeta TESTS = $(check_PROGRAMS) test-luksmeta latchset-luksmeta-9e93e21/README.md000066400000000000000000000071761510111145300167710ustar00rootroot00000000000000[![License](http://img.shields.io/:license-lgpl2-blue.svg?style=flat-square)](http://www.gnu.org/licenses/lgpl-2.1.html) [![Build Status](https://travis-ci.org/latchset/luksmeta.svg?branch=master)](https://travis-ci.org/latchset/luksmeta) [![Code Coverage](http://codecov.io/github/latchset/luksmeta/coverage.svg?branch=master)](http://codecov.io/github/latchset/luksmeta?branch=master) [![Coverity Scan Build Status](https://scan.coverity.com/projects/8706/badge.svg)](https://scan.coverity.com/projects/latchset-luksmeta) # LUKSMeta Welcome to LUKSMeta! LUKSMeta is a simple library for storing metadata in the LUKSv1 header. This library is licensed under the GNU LGPLv2+. ## Why LUKSMeta? Some projects need to store additional metadata about a LUKS volume that is accessable before unlocking it. Two such examples are [USBGuard][usbguard] and [Tang][tang]. Fortunately, there is a gap in the LUKS header between the end of the slot area and the payload offset: +---------------+------------------+-----------------------------------------+----------------+ | LUKSv1 header | LUKSv1 slots (8) | | Encrypted Data | +---------------+------------------+-----------------------------------------+----------------+ LUKSMeta uses this hole to store additional metadata. ## How does LUKSMeta work? LUKSMeta's on-disk format consists of a header block, followed by 0-8 data blocks. Each block is aligned to 4096 bytes. The LUKSMeta header contains a checksum (CRC32c) of itself and of each data block to detect data corruption. Each data block is also given a 16 byte UUID type to uniquely identify the contents of the block. The end result looks like this on disk: +---------------+------------------+-----------------+-----------------------+----------------+ | LUKSv1 header | LUKSv1 slots (8) | LUKSMeta header | LUKSMeta blocks (0-8) | Encrypted Data | +---------------+------------------+-----------------+-----------------------+----------------+ ## LUKSMeta Command Line Interface luksmeta test -d DEVICE luksmeta nuke -d DEVICE [-f] luksmeta init -d DEVICE [-f] [-n] luksmeta show -d DEVICE [-s SLOT] luksmeta save -d DEVICE [-s SLOT] -u UUID < DATA luksmeta load -d DEVICE -s SLOT [-u UUID] > DATA luksmeta wipe -d DEVICE -s SLOT [-u UUID] [-f] ### Examples Destroy all data (including LUKSMeta data) in the LUKSv1 header gap and initalize the gap for LUKSMeta storage: $ luksmeta init -n -d /dev/sdz You are about to initialize a LUKS device for metadata storage. Attempting to initialize it may result in data loss if data was already written into the LUKS header gap in a different format. A backup is advised before initialization is performed. Do you wish to initialize /dev/sdz? [yn] y If already initialized, do nothing. Otherwise, destroy all non-LUKSMeta data in the LUKSv1 header gap and initialize the gap for LUKSMeta storage. Skip user confirmation (dangerous!): $ luksmeta init -f -d /dev/sdz Write some data to a slot: $ UUID=`uuidgen` $ echo $UUID 31c25e3b-b8e2-4eaa-a427-23aa882feef2 $ echo "Hello, World" | luksmeta save -d /dev/sdz -s 0 -u $UUID Read the data back: $ luksmeta load -d /dev/sdz -s 0 -u $UUID Hello, World Wipe the data from the slot: $ luksmeta wipe -d /dev/sdz -s 0 -u $UUID Erase all trace of LUKSMeta: $ luksmeta nuke -d /dev/sdz You are about to erase all data in the LUKSMeta storage area. A backup is advised before erasure is performed. Do you wish to nuke /dev/sdz? [yn] y [usbguard]: https://github.com/dkopecek/usbguard [tang]: https://github.com/latchset/tang latchset-luksmeta-9e93e21/configure.ac000066400000000000000000000016331510111145300177700ustar00rootroot00000000000000AC_PREREQ(2.59) AC_INIT(luksmeta, 10) AC_CANONICAL_SYSTEM AC_PROG_CC_C99 AM_INIT_AUTOMAKE([subdir-objects foreign no-dist-gzip dist-bzip2 parallel-tests]) AM_SILENT_RULES([yes]) AM_PROG_CC_C_O LT_INIT([disable-static]) PKG_PROG_PKG_CONFIG([0.25]) PKG_CHECK_MODULES([cryptsetup], [libcryptsetup >= 1.5.1]) LUKSMETA_CFLAGS="\ -Wall \ -Wextra \ -Werror \ -Wstrict-aliasing \ -Wchar-subscripts \ -Wformat-security \ -Wmissing-declarations \ -Wmissing-prototypes \ -Wnested-externs \ -Wpointer-arith \ -Wshadow \ -Wsign-compare \ -Wstrict-prototypes \ -Wtype-limits \ -Wunused-function \ -Wno-missing-field-initializers \ -Wno-unused-parameter \ " AC_SUBST([LUKSMETA_CFLAGS]) AC_CHECK_PROGS(A2X, [a2x]) if test "x$A2X" = "x"; then AC_MSG_WARN([asciidoc / a2x not found -- man pages will not be generated and installed!]) fi AM_CONDITIONAL(HAVE_A2X, [test -n "$A2X"]) AC_CONFIG_FILES([luksmeta.pc Makefile]) AC_OUTPUT latchset-luksmeta-9e93e21/crc32c.c000066400000000000000000000104571510111145300167310ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "crc32c.h" static const uint32_t table[] = { 0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384, 0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35, 0xaa64d611, 0x580f5512, 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa, 0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad, 0x1642ae59, 0xe4292d5a, 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595, 0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957, 0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f, 0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927, 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f, 0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7, 0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789, 0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46, 0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6, 0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de, 0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x082f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c, 0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6, 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d, 0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0x0e330a81, 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed, 0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8, 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540, 0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f, 0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1, 0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e, 0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351, }; uint32_t crc32c(uint32_t crc, const void *buf, size_t len) { const uint8_t *tmp = buf; crc ^= 0xffffffffUL; while (len--) crc = table[(crc ^ *tmp++) & 0xff] ^ (crc >> 8); crc ^= 0xffffffffUL; return crc; } latchset-luksmeta-9e93e21/crc32c.h000066400000000000000000000017141510111145300167320ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #pragma once #include #include uint32_t crc32c(uint32_t crc, const void *buf, size_t len); latchset-luksmeta-9e93e21/libluksmeta.c000066400000000000000000000266061510111145300201710ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "crc32c.h" #include "luksmeta.h" #include #include #include #include #include #include #include #include #define ALIGN(s, up) (((s) + (up ? 4095 : 0)) & ~4095ULL) #define LUKS_NSLOTS 8 #define LM_VERSION 1 static const uint8_t LM_MAGIC[] = { 'L', 'U', 'K', 'S', 'M', 'E', 'T', 'A' }; typedef struct __attribute__((packed)) { luksmeta_uuid_t uuid; uint32_t offset; /* Bytes from the start of the hole */ uint32_t length; /* Bytes */ uint32_t crc32c; uint32_t _reserved; /* Reserved */ } lm_slot_t; typedef struct __attribute__((packed)) { uint8_t magic[sizeof(LM_MAGIC)]; uint32_t version; uint32_t crc32c; lm_slot_t slots[LUKS_NSLOTS]; } lm_t; static bool uuid_is_zero(const luksmeta_uuid_t uuid) { for (size_t i = 0; i < sizeof(luksmeta_uuid_t); i++) { if (uuid[i] != 0) return false; } return true; } static inline uint32_t checksum(lm_t lm) { lm.crc32c = 0; return crc32c(0, &lm, sizeof(lm_t)); } static inline bool overlap(const lm_t *lm, uint32_t start, size_t end, uint32_t hard_limit) { /* Make sure the data fits the available area in the gap. */ if (end > hard_limit) return true; for (int i = 0; i < LUKS_NSLOTS; i++) { const lm_slot_t *s = &lm->slots[i]; uint32_t e = s->offset + s->length; if (start <= s->offset && s->offset < end) return true; if (start < e && e <= end) return true; } return false; } static inline uint32_t find_gap(const lm_t *lm, uint32_t length, size_t size) { size = ALIGN(size, true); /* Make sure the data is not larger than the total available * area in the gap. */ if (length < size) return 0; for (uint32_t off = ALIGN(1, true); off < length; off += ALIGN(1, true)) { if (!overlap(lm, off, off + size, lm->slots[0].offset + length)) return off; } return 0; } static int find_unused_slot(struct crypt_device *cd, const lm_t *lm) { for (int slot = 0; slot < LUKS_NSLOTS; slot++) { if (crypt_keyslot_status(cd, slot) == CRYPT_SLOT_INACTIVE && uuid_is_zero(lm->slots[slot].uuid)) return slot; } return -1; } static inline ssize_t readall(int fd, void *data, size_t size) { uint8_t *tmp = data; for (ssize_t r, t = 0; t < (ssize_t) size; t += r) { r = read(fd, &tmp[t], size - t); if (r < 0 && errno != EAGAIN) return -errno; if (r == 0) return -ENOENT; } return size; } static inline ssize_t writeall(int fd, const void *buf, size_t size) { const uint8_t *tmp = buf; for (ssize_t r, t = 0; t < (ssize_t) size; t += r) { r = write(fd, &tmp[t], size - t); if (r < 0) { if (errno != EAGAIN) return -errno; r = 0; } } return size; } /** * Opens the device with the specified flags. * * The length parameter is set to the amount of space in the gap between the * end of the last slot and the start of the encrypted data. * * The function returns either the file descriptor positioned to the start of * the hole or a negative errno. */ static int open_hole(struct crypt_device *cd, int flags, uint32_t *length) { const char *name = NULL; const char *type = NULL; uint64_t hole = 0; uint64_t data = 0; int fd = 0; int r = 0; type = crypt_get_type(cd); if (!type || strcmp(CRYPT_LUKS1, type) != 0) return -ENOTSUP; data = crypt_get_data_offset(cd) * 512; if (data < 4096) return -ENOSPC; for (int slot = 0; slot < LUKS_NSLOTS; slot++) { uint64_t off = 0; uint64_t len = 0; r = crypt_keyslot_area(cd, slot, &off, &len); if (r < 0) return r; if (hole < off + len) hole = ALIGN(off + len, true); } if (hole == 0) return -ENOTSUP; if (hole >= data) return -ENOSPC; name = crypt_get_device_name(cd); if (!name) return -ENOTSUP; fd = open(name, flags); if (fd < 0) return -errno; if (lseek(fd, hole, SEEK_SET) == -1) { close(fd); return -errno; } *length = ALIGN(data - hole, false); return fd; } static int read_header(struct crypt_device *cd, int flags, uint32_t *length, lm_t *lm) { uint32_t maxlen; int fd = -1; int r = 0; fd = open_hole(cd, flags, length); if (fd < 0) return fd; r = *length >= sizeof(lm_t) ? 0 : -ENOENT; if (r < 0) goto error; r = readall(fd, lm, sizeof(lm_t)); if (r < 0) goto error; r = memcmp(LM_MAGIC, lm->magic, sizeof(LM_MAGIC)) == 0 ? 0 : -ENOENT; if (r < 0) goto error; r = lm->version == htobe32(LM_VERSION) ? 0 : -ENOTSUP; if (r < 0) goto error; lm->crc32c = be32toh(lm->crc32c); r = checksum(*lm) == lm->crc32c ? 0 : -EINVAL; if (r < 0) goto error; lm->version = be32toh(lm->version); maxlen = *length - ALIGN(sizeof(lm_t), true); for (int slot = 0; slot < LUKS_NSLOTS; slot++) { lm_slot_t *s = &lm->slots[slot]; s->offset = be32toh(s->offset); s->length = be32toh(s->length); s->crc32c = be32toh(s->crc32c); if (!uuid_is_zero(s->uuid)) { r = s->offset > sizeof(lm_t) ? 0 : -EINVAL; if (r < 0) goto error; r = s->length <= maxlen ? 0 : -EINVAL; if (r < 0) goto error; } } return fd; error: close(fd); return r; } static int write_header(int fd, lm_t lm) { for (int slot = 0; slot < LUKS_NSLOTS; slot++) { lm.slots[slot].offset = htobe32(lm.slots[slot].offset); lm.slots[slot].length = htobe32(lm.slots[slot].length); lm.slots[slot].crc32c = htobe32(lm.slots[slot].crc32c); } memcpy(lm.magic, LM_MAGIC, sizeof(LM_MAGIC)); lm.version = htobe32(LM_VERSION); lm.crc32c = htobe32(checksum(lm)); return writeall(fd, &lm, sizeof(lm)); } int luksmeta_test(struct crypt_device *cd) { int fd = -1; fd = read_header(cd, O_RDONLY, &(uint32_t) {0}, &(lm_t) {}); if (fd >= 0) { close(fd); return 0; } return fd; } int luksmeta_nuke(struct crypt_device *cd) { uint8_t zero[ALIGN(1, true)] = {}; uint32_t length = 0; int fd = -1; int r = 0; fd = open_hole(cd, O_RDWR | O_SYNC, &length); if (fd < 0) return fd; for (size_t i = 0; r >= 0 && i < length; i += sizeof(zero)) r = writeall(fd, zero, sizeof(zero)); close(fd); return r < 0 ? r : 0; } int luksmeta_init(struct crypt_device *cd) { uint32_t length = 0; int fd = -1; int r = 0; r = luksmeta_test(cd); if (r == 0) return -EALREADY; else if (r != -ENOENT && r != -EINVAL) return r; fd = open_hole(cd, O_RDWR | O_SYNC, &length); if (fd < 0) return fd; if (length < ALIGN(sizeof(lm_t), true)) { close(fd); return -ENOSPC; } r = write_header(fd, (lm_t) {}); close(fd); return r > 0 ? 0 : r; } int luksmeta_load(struct crypt_device *cd, int slot, luksmeta_uuid_t uuid, void *buf, size_t size) { uint32_t length = 0; lm_slot_t *s = NULL; lm_t lm = {}; int fd = -1; int r = 0; if (slot < 0 || slot >= LUKS_NSLOTS) return -EBADSLT; s = &lm.slots[slot]; fd = read_header(cd, O_RDONLY, &length, &lm); if (fd < 0) return fd; r = uuid_is_zero(s->uuid) ? -ENODATA : 0; if (r < 0) goto error; if (buf) { r = size >= s->length ? 0 : -E2BIG; if (r < 0) goto error; r = lseek(fd, s->offset - sizeof(lm), SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = readall(fd, buf, s->length); if (r < 0) goto error; r = crc32c(0, buf, s->length) == s->crc32c ? 0 : -EINVAL; if (r < 0) goto error; } memcpy(uuid, s->uuid, sizeof(luksmeta_uuid_t)); close(fd); return s->length; error: close(fd); return r; } int luksmeta_save(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid, const void *buf, size_t size) { uint32_t length = 0; lm_slot_t *s = NULL; lm_t lm = {}; int fd = -1; int r = 0; off_t off; if (uuid_is_zero(uuid)) return -EKEYREJECTED; fd = read_header(cd, O_RDWR | O_SYNC, &length, &lm); if (fd < 0) return fd; if (slot == CRYPT_ANY_SLOT) slot = find_unused_slot(cd, &lm); r = slot >= 0 && slot < LUKS_NSLOTS ? 0 : -EBADSLT; if (r < 0) goto error; s = &lm.slots[slot]; r = uuid_is_zero(s->uuid) ? 0 : -EALREADY; if (r < 0) goto error; s->offset = find_gap(&lm, length, size); r = s->offset >= ALIGN(sizeof(lm), true) ? 0 : -ENOSPC; if (r < 0) goto error; memcpy(s->uuid, uuid, sizeof(luksmeta_uuid_t)); s->length = size; s->crc32c = crc32c(0, buf, size); off = s->offset - sizeof(lm); r = lseek(fd, off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = writeall(fd, buf, size); if (r < 0) goto error; off = s->offset + s->length; r = lseek(fd, -off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = write_header(fd, lm); error: close(fd); return r < 0 ? r : slot; } int luksmeta_wipe(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid) { uint8_t *zero = NULL; uint32_t length = 0; lm_slot_t *s = NULL; lm_t lm = {}; int fd = -1; int r = 0; off_t off; if (slot < 0 || slot >= LUKS_NSLOTS) return -EBADSLT; s = &lm.slots[slot]; fd = read_header(cd, O_RDWR | O_SYNC, &length, &lm); if (fd < 0) return fd; r = uuid_is_zero(s->uuid) ? -EALREADY : 0; if (r < 0) goto error; if (uuid && memcmp(uuid, s->uuid, sizeof(luksmeta_uuid_t)) != 0) { r = -EKEYREJECTED; goto error; } off = s->offset - sizeof(lm_t); r = lseek(fd, off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; r = (zero = calloc(1, s->length)) ? 0 : -errno; if (r < 0) goto error; r = writeall(fd, zero, s->length); free(zero); if (r < 0) goto error; off = s->offset + s->length; r = lseek(fd, -off, SEEK_CUR) == -1 ? -errno : 0; if (r < 0) goto error; memset(s, 0, sizeof(lm_slot_t)); r = write_header(fd, lm); error: close(fd); return r < 0 ? r : 0; } latchset-luksmeta-9e93e21/luksmeta.8.adoc000066400000000000000000000225741510111145300203340ustar00rootroot00000000000000luksmeta(8) =========== :doctype: manpage == NAME luksmeta - Utility for storing metadata in a LUKSv1 header == SYNOPSIS *luksmeta test* -d DEVICE *luksmeta nuke* -d DEVICE [-f] *luksmeta init* -d DEVICE [-f] [-n] *luksmeta show* -d DEVICE [-s SLOT] *luksmeta save* -d DEVICE [-s SLOT] -u UUID < DATA *luksmeta load* -d DEVICE -s SLOT [-u UUID] > DATA *luksmeta wipe* -d DEVICE -s SLOT [-u UUID] [-f] == OVERVIEW The *luksmeta* utility enables an administrator to store metadata in the gap between the end of the LUKSv1 header and the start of the encrypted data. This is useful for storing data that is available before the volume is unlocked, usually for use during the volume unlock process. The metadata is stored in a series of UUID-typed slots, allowing multiple blocks of metadata. Although the *luksmeta* slots are inspired by the LUKS slots, they are functionally independent and share only a casual relationship. Slots merely provide a hint that a given chunk of metadata is associated with a specific LUKSv1 password (in a slot with the same number). However, *luksmeta* itself is indifferent to the relationship between a LUKSv1 slot and the correspondly numbered *luksmeta* slot, with one exception (detailed below). After a LUKSv1 volume is initialized using *cryptsetup*(8), it must also be initialized for metadata storage by *luksmeta init*. Once this is complete, the device is ready to store medata. Data can be written to a slot using *luksmeta save* or read from a slot using *luksmeta load*. You can also erase the data in an existing slot using *luksmeta wipe* or query the slots using *luksmeta show*. == UUID GENERATION It is often presumed that saving metadata to a slot requires a specific UUID or that there is an official registry of UUID types. This is incorrect. UUID stands for Universally Unique IDentifier. UUIDs are a standardized, widely-used data type used for identification without a central registry. For the relevant standards, see ISO 9834-8:2005 and RFC 4122. UUIDs are large enough that collision is practically impossible. So if your application wants to store data in a *luksmeta* slot, just generate your own UUID and use it consistently to refer to your type of data. If you have multiple types of data, feel free to generate multiple UUIDs. The easiest way to generate a UUID is to use *uuidgen*(1). However, any compliant UUID generator will suffice. == INITIALIZATION Before reading or writing metadata, the LUKSv1 block device must be initialized for metadata storage. Three commands help with this process: *luksmeta test*, *luksmeta nuke* and *luksmeta init*. The *luksmeta test* command simply checks an existing block device to see if it is initialized for metadata storage. This command does not provide any output, so be sure to check its return code (see below). The *luksmeta nuke* command will zero (erase) the entire LUKSv1 header gap. Since this operation is destructive, user confirmation will be required before clearing the gap unless the *-f* option is supplied. The *luksmeta init* command initializes the LUKSv1 block device for metadata storage. This process will wipe out any data in the LUKSv1 header gap. For this reason, this command will require user confirmation before any data is written unless the *-f* option is supplied. Note that this command succeeds without any modification if the device is already initialized. If you would like to force the creation of clean initialization state, you can specify the *-n* option to nuke the LUKSv1 header gap before initialization (but after user confirmation). == METADATA STATE The *luksmeta show* command displays the current state of slots on the LUKSv1 block device. If no slot is specified, it prints a table consisting of the slot number, the corresponding LUKSv1 slot state and the UUID of the data stored in the *luksmeta* slot (or "empty" if no data is stored). If a slot is specified, this command simply prints out the UUID of the data in the slot. If the slot does not contain data, it prints nothing. == MANAGING METADATA Managing the metadata in the slots is performed with three commands: *luksmeta save*, *luksmeta load* and *luksmeta wipe*. These commands write metadata to a slot, read metadata from a slot and erase metadata in a slot, respectively. The *luksmeta save* command reads metadata on standard input and writes it to the specified slot using the specified UUID. If no slot is specified, *luksmeta* will search for the first slot number for which the LUKSv1 slot is inactive and the *luksmeta* slot is empty. This represents the only official correlation between LUKSv1 slots and *luksmeta* slots. In this case, the metadata is written to the first applicable slot using the specified UUID and the slot number is printed to standard output. In either case, this command will never overwrite existing data. To replace data in a slot you will need to execute *luksmeta wipe* before *luksmeta save*. The *luksmeta load* command reads data from the specified slot and writes it to standard output. If a UUID is specified, the command will verify that the UUID associated with the metadata in the slot matches the specified UUID. This type check helps to ensure that you always receive the type of data you are expecting as output. If the UUIDs do not match, the command will fail. The *luksmeta wipe* command erases the data from the given slot. If a UUID is specified, the command will verify that the UUID associated with the metadata in the slot matches the specified UUID. This type check helps to ensure that you only erase the data you intended to erase. Because this is a destructive operation, this command will require user confirmation before any data is erased, unless the *-f* option is supplied. Note that this command succeeds if you attempt to wipe a slot that is already empty. == CAVEATS The amount of storage in the LUKSv1 header gap is extremely limited. It also varies based upon the configuration used by LUKSv1 at device initialization time. In some LUKSv1 configurations, there is not even enough space for all the metadata slots even at the smallest possible slot size. During the design of this utility, we considered it likely that users would want to reduce the number of usable slots in exchange for more storage space in the slots used. In order to provide this flexibility, the amount of storage available per-slot is dynamic. Put simply, slots are not a fixed size. This means that it is possible (and even somewhat likely) to encounter an error during *luksmeta save* indicating that there is insufficient space. This error is not a programming bug. If you encounter this error it likely means that either all space is being consumed by the already-written slots or that the metadata you are attempting to write simply does not fit. You can attempt to resolve this problem by calling *luksmeta wipe* on slots that are no longer in use. This will release the storage space for use by other slots. Note that *luksmeta* does not, however, currently perform defragmentation since the number of usable blocks is rather limited. You can attempt to manually get around this by extracting all slot data, wiping the slots and reloading them in order. However, this operation is potentially dangerous and should be undertaken with great care. == OPTIONS * *-d* _DEVICE_, *--device*=_DEVICE_ : The device on which to perform the operation. * *-s* _SLOT_, *--slot*=_SLOT_ : The slot number on which to perform the operation. * *-u* _UUID_, *--uuid*=_UUID_ : The UUID to associate with the operation. * *-f*, *--force* : Forcibly suppress all user prompting. == RETURN VALUES This command uses the return values as defined by *sysexits.h*. The following are general errors (and corresponding numeric exit codes) whose meaning is shared by all *luksmeta* commands: * *EX_OK* (0): The operation was successful. * *EX_USAGE* (64): The program was called with invalid parameters. * *EX_DATAERR* (65): The specified UUID does not match the slot UUID. * *EX_NOINPUT* (66): An error occurred while reading from standard input. * *EX_OSERR* (71): An undefined operating system error occurred. * *EX_OSFILE* (72): The device is not initialized or is corrupted. * *EX_CANTCREAT* (73): There is insufficient space in LUKSv1 header. * *EX_IOERR* (74): An IO error occurred when writing to the device. * *EX_NOPERM* (77): The user did not grant permission during confirmation. Additionally, *luksmeta save* will return *EX_UNAVAILABLE* when you attempt to save data into a slot that is already used. Likewise, *luksmeta load* will return *EX_UNAVAILABLE* when you attempt to read from an empty slot. == EXAMPLES Destroy all data (including LUKSMeta data) in the LUKSv1 header gap and initialize the gap for LUKSMeta storage: $ luksmeta init -n -f -d /dev/sdz If already initialized, do nothing. Otherwise, destroy all non-LUKSMeta data in the LUKSv1 header gap and initialize the gap for LUKSMeta storage: $ luksmeta init -f -d /dev/sdz Write some data to a slot: $ UUID=*uuidgen* $ echo $UUID 31c25e3b-b8e2-4eaa-a427-23aa882feef2 $ echo "Hello, World" | luksmeta save -d /dev/sdz -s 0 -u $UUID Read the data back: $ luksmeta load -d /dev/sdz -s 0 -u $UUID Hello, World Wipe the data from the slot: $ luksmeta wipe -d /dev/sdz -s 0 -u $UUID Erase all trace of LUKSMeta: $ luksmeta nuke -f -d /dev/sdz == AUTHOR Nathaniel McCallum == SEE ALSO *cryptsetup*(8), *uuidgen*(1) latchset-luksmeta-9e93e21/luksmeta.c000066400000000000000000000351041510111145300174730ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "luksmeta.h" #include #include #include #include #include #include #include #define UUID_TMPL \ "%02hhx%02hhx%02hhx%02hhx-" \ "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-" \ "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" #define UUID_ARGS(u) \ u[0x0], u[0x1], u[0x2], u[0x3], u[0x4], u[0x5], u[0x6], u[0x7], \ u[0x8], u[0x9], u[0xa], u[0xb], u[0xc], u[0xd], u[0xe], u[0xf] struct options { const char *device; luksmeta_uuid_t uuid; bool have_uuid; bool force; bool nuke; int slot; }; #define LUKSMETA_LIBCRYPTSETUP_LOG_LEVEL CRYPT_LOG_ERROR static void luksmeta_libcryptsetup_log(int level, const char *msg, void *usrptr) { if (level != LUKSMETA_LIBCRYPTSETUP_LOG_LEVEL) { return; } fprintf(stderr, "%s", msg); } static int cmd_test(const struct options *opts, struct crypt_device *cd) { return luksmeta_test(cd) == 0 ? EX_OK : EX_OSFILE; } static int cmd_nuke(const struct options *opts, struct crypt_device *cd) { int r = 0; if (!opts->force) { int c = 'X'; fprintf(stderr, "You are about to erase all data in the LUKSMeta storage area.\n" "A backup is advised before erasure is performed.\n\n"); while (!strchr("YyNn", c)) { fprintf(stderr, "Do you wish to nuke %s? [yn] ", crypt_get_device_name(cd)); c = getc(stdin); } if (strchr("Nn", c)) return EX_NOPERM; } r = luksmeta_nuke(cd); switch (r) { case 0: return EX_OK; default: fprintf(stderr, "Error while erasing device (%s): %s\n", opts->device, strerror(-r)); return EX_OSERR; } } static int cmd_init(const struct options *opts, struct crypt_device *cd) { int r = 0; if (!opts->force) { int c = 'X'; fprintf(stderr, "You are about to initialize a LUKS device for metadata storage.\n" "Attempting to initialize it may result in data loss if data was\n" "already written into the LUKS header gap in a different format.\n" "A backup is advised before initialization is performed.\n\n"); while (!strchr("YyNn", c)) { fprintf(stderr, "Do you wish to initialize %s? [yn] ", crypt_get_device_name(cd)); c = getc(stdin); } if (strchr("Nn", c)) return EX_NOPERM; } if (opts->nuke) { struct options o = { .force = true, .device = opts->device }; r = cmd_nuke(&o, cd); if (r != EX_OK) return r; } r = luksmeta_init(cd); switch (r) { case 0: /* fallthrough */ case -EALREADY: return EX_OK; case -ENOSPC: fprintf(stderr, "Insufficient space in the LUKS header (%s)\n", opts->device); return EX_CANTCREAT; default: fprintf(stderr, "Error while initializing device (%s): %s\n", opts->device, strerror(-r)); return EX_OSERR; } return EX_OK; } static const char * status(struct crypt_device *cd, int keyslot) { switch (crypt_keyslot_status(cd, keyslot)) { case CRYPT_SLOT_INVALID: return "invalid"; case CRYPT_SLOT_INACTIVE: return "inactive"; case CRYPT_SLOT_ACTIVE: return "active"; case CRYPT_SLOT_ACTIVE_LAST: return "active"; default: return "unknown"; } } static int cmd_show(const struct options *opts, struct crypt_device *cd) { luksmeta_uuid_t uuid = {}; for (int i = 0, r = 0; i < crypt_keyslot_max(CRYPT_LUKS1); i++) { if (opts->slot >= 0 && i != opts->slot) continue; r = luksmeta_load(cd, i, uuid, NULL, 0); switch (r) { case -EBADSLT: fprintf(stderr, "Invalid slot (%d)\n", opts->slot); return EX_USAGE; case -ENOENT: fprintf(stderr, "Device is not initialized (%s)\n", opts->device); return EX_OSFILE; case -EINVAL: fprintf(stderr, "LUKSMeta data appears corrupt (%s)\n", opts->device); return EX_OSFILE; case -ENODATA: if (opts->slot < 0) fprintf(stdout, "%d %8s %s\n", i, status(cd, i), "empty"); break; default: if (r < 0) { fprintf(stderr, "%d %8s %s\n", i, status(cd, i), "unknown error"); } else { if (opts->slot < 0) fprintf(stdout, "%d %8s ", i, status(cd, i)); fprintf(stdout, UUID_TMPL "\n", UUID_ARGS(uuid)); } break; } } return EX_OK; } static int cmd_save(const struct options *opts, struct crypt_device *cd) { uint8_t *in = NULL; size_t inl = 0; int r = 0; if (!opts->have_uuid) { fprintf(stderr, "UUID required\n"); return EX_USAGE; } while (!feof(stdin)) { uint8_t *tmp = NULL; tmp = realloc(in, inl + 4096); if (!tmp) { fprintf(stderr, "Out of memory\n"); free(in); return EX_OSERR; } in = tmp; r = fread(&in[inl], 1, 4096, stdin); inl += r; if (r < 4096 && (ferror(stdin) || inl == 0)) { fprintf(stderr, "Error reading from standard input\n"); free(in); return EX_NOINPUT; } } if (!in) { fprintf(stderr, "No data on standard input\n"); return EX_NOINPUT; } r = luksmeta_save(cd, opts->slot, opts->uuid, in, inl); memset(in, 0, inl); free(in); switch (r) { case -ENOENT: fprintf(stderr, "Device is not initialized (%s)\n", opts->device); return EX_OSFILE; case -EINVAL: fprintf(stderr, "LUKSMeta data appears corrupt (%s)\n", opts->device); return EX_OSFILE; case -EBADSLT: fprintf(stderr, "The specified slot is invalid (%d)\n", opts->slot); return EX_USAGE; case -EKEYREJECTED: fprintf(stderr, "The specified UUID is reserved (" UUID_TMPL ")\n", UUID_ARGS(opts->uuid)); return EX_USAGE; case -EALREADY: fprintf(stderr, "Will not overwrite existing slot (%d)\n", opts->slot); return EX_UNAVAILABLE; case -ENOSPC: fprintf(stderr, "Insufficient space in the LUKS header (%s)\n", opts->device); return EX_CANTCREAT; default: if (r < 0) fprintf(stderr, "An unknown error occurred\n"); else if (opts->slot < 0) fprintf(stdout, "%d\n", r); return r < 0 ? EX_OSERR : EX_OK; } } static int cmd_load(const struct options *opts, struct crypt_device *cd) { luksmeta_uuid_t uuid = {}; int r = 0; if (opts->slot < 0) { fprintf(stderr, "Slot required\n"); return EX_USAGE; } r = luksmeta_load(cd, opts->slot, uuid, NULL, 0); if (r >= 0) { uint8_t *out = NULL; if (opts->have_uuid && memcmp(opts->uuid, uuid, sizeof(uuid)) != 0) { fprintf(stderr, "The given UUID does not match the slot UUID:\n" "UUID: " UUID_TMPL "\n" "SLOT: " UUID_TMPL "\n", UUID_ARGS(opts->uuid), UUID_ARGS(uuid)); return EX_DATAERR; } out = malloc(r); if (!out) { fprintf(stderr, "Out of memory!\n"); return EX_OSERR; } r = luksmeta_load(cd, opts->slot, uuid, out, r); if (r >= 0) { fwrite(out, 1, r, stdout); memset(out, 0, r); } free(out); } switch (r) { case -ENOENT: fprintf(stderr, "Device is not initialized (%s)\n", opts->device); return EX_OSFILE; case -EINVAL: fprintf(stderr, "LUKSMeta data appears corrupt (%s)\n", opts->device); return EX_OSFILE; case -EBADSLT: fprintf(stderr, "The specified slot is invalid (%d)\n", opts->slot); return EX_USAGE; case -ENODATA: fprintf(stderr, "The specified slot is empty (%d)\n", opts->slot); return EX_UNAVAILABLE; default: if (r < 0) fprintf(stderr, "An unknown error occurred\n"); return r < 0 ? EX_OSERR : EX_OK; } } static int cmd_wipe(const struct options *opts, struct crypt_device *cd) { luksmeta_uuid_t uuid = {}; int r = 0; if (opts->slot < 0) { fprintf(stderr, "Slot required\n"); return EX_USAGE; } if (!opts->force) { int c = 'X'; fprintf(stderr, "You are about to wipe a slot. This operation is unrecoverable.\n" "A backup is advised before proceeding.\n\n"); while (!strchr("YyNn", c)) { fprintf(stderr, "Do you wish to erase slot %d on %s? [yn] ", opts->slot, crypt_get_device_name(cd)); c = getc(stdin); } if (strchr("Nn", c)) return EX_NOPERM; } r = luksmeta_wipe(cd, opts->slot, opts->have_uuid ? opts->uuid : NULL); switch (r) { case -EALREADY: return EX_OK; case -ENOENT: fprintf(stderr, "Device is not initialized (%s)\n", opts->device); return EX_OSFILE; case -EINVAL: fprintf(stderr, "LUKSMeta data appears corrupt (%s)\n", opts->device); return EX_OSFILE; case -EBADSLT: fprintf(stderr, "The specified slot is invalid (%d)\n", opts->slot); return EX_USAGE; case -EKEYREJECTED: r = luksmeta_load(cd, opts->slot, uuid, NULL, 0); if (r >= 0) { fprintf(stderr, "The given UUID does not match the slot UUID:\n" "UUID: " UUID_TMPL "\n" "SLOT: " UUID_TMPL "\n", UUID_ARGS(opts->uuid), UUID_ARGS(uuid)); } else { fprintf(stderr, "The given UUID does not match the slot UUID:\n" "UUID: " UUID_TMPL "\n" "SLOT: UNKNOWN\n", UUID_ARGS(opts->uuid)); } return EX_DATAERR; default: if (r < 0) fprintf(stderr, "An unknown error occurred\n"); return r < 0 ? EX_OSERR : EX_OK; } } static const char *sopts = "hfnd:u:s:"; static const struct option lopts[] = { { "help", .val = 'h' }, { "nuke", no_argument, .val = 'n' }, { "force", no_argument, .val = 'f' }, { "device", required_argument, .val = 'd' }, { "uuid", required_argument, .val = 'u' }, { "slot", required_argument, .val = 's' }, {} }; static const struct { int (*func)(const struct options *opts, struct crypt_device *cd); const char *name; } commands[] = { { cmd_test, "test", }, { cmd_nuke, "nuke", }, { cmd_init, "init", }, { cmd_show, "show", }, { cmd_save, "save", }, { cmd_load, "load", }, { cmd_wipe, "wipe", }, {} }; int main(int argc, char *argv[]) { struct options o = { .slot = CRYPT_ANY_SLOT }; for (int c; (c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1; ) { switch (c) { case 'h': goto usage; case 'd': o.device = optarg; break; case 'n': o.nuke = true; break; case 'f': o.force = true; break; case 'u': if (sscanf(optarg, UUID_TMPL, UUID_ARGS(&o.uuid)) != 16) { fprintf(stderr, "Invalid UUID (%s)\n", optarg); return EX_USAGE; } o.have_uuid = true; break; case 's': if (sscanf(optarg, "%d", &o.slot) != 1 || o.slot < 0 || o.slot >= crypt_keyslot_max(CRYPT_LUKS1)) { fprintf(stderr, "Invalid slot (%s)\n", optarg); return EX_USAGE; } break; } } if (argc > 1 && !o.device) { fprintf(stderr, "Device must be specified\n\n"); goto usage; } if (optind != argc - 1) goto usage; for (size_t i = 0; argc > 1 && commands[i].name; i++) { struct crypt_device *cd = NULL; const char *type = NULL; int r = 0; if (strcmp(argv[optind], commands[i].name) != 0) continue; r = crypt_init(&cd, o.device); if (r != 0) { fprintf(stderr, "Unable to open device (%s): %s\n", o.device, strerror(-r)); return EX_IOERR; } crypt_set_log_callback(cd, luksmeta_libcryptsetup_log, NULL); r = crypt_load(cd, NULL, NULL); if (r != 0) { fprintf(stderr, "Unable to read LUKSv1 header (%s): %s\n", o.device, strerror(-r)); crypt_free(cd); return EX_IOERR; } type = crypt_get_type(cd); if (type == NULL) { fprintf(stderr, "Unable to determine device type for %s\n", o.device); crypt_free(cd); return EX_OSFILE; } if (strcmp(type, CRYPT_LUKS1) != 0) { fprintf(stderr, "%s (%s) is not a LUKSv1 device\n", o.device, type); crypt_free(cd); return EX_OSFILE; } r = commands[i].func(&o, cd); crypt_free(cd); return r; } fprintf(stderr, "Invalid command\n\n"); usage: fprintf(stderr, "Usage: luksmeta test -d DEVICE\n" " or: luksmeta nuke -d DEVICE [-f]\n" " or: luksmeta init -d DEVICE [-f] [-n]\n" " or: luksmeta show -d DEVICE [-s SLOT]\n" " or: luksmeta save -d DEVICE [-s SLOT] -u UUID < DATA\n" " or: luksmeta load -d DEVICE -s SLOT [-u UUID] > DATA\n" " or: luksmeta wipe -d DEVICE -s SLOT [-u UUID] [-f]\n"); return EX_USAGE; } latchset-luksmeta-9e93e21/luksmeta.h000066400000000000000000000107251510111145300175020ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #pragma once #include #ifdef __cplusplus extern "C" { #endif typedef uint8_t luksmeta_uuid_t[16]; /** * Checks for the existence of a valid LUKSMeta header on a LUKSv1 device * * @param cd crypt device handle * @return Zero on success or negative errno value otherwise. * * @note This function returns -ENOENT if the device has no luksmeta header. * @note This function returns -EINVAL if the header or slot data is corrupted. */ int luksmeta_test(struct crypt_device *cd); /** * Zeroes the entire LUKSMeta storage space. * * @param cd crypt device handle * @return Zero on success or negative errno value otherwise. */ int luksmeta_nuke(struct crypt_device *cd); /** * Initializes metadata storage on a LUKSv1 device * * @param cd crypt device handle * @return Zero on success or negative errno value otherwise. * * @note This function returns -EALREADY if a valid header already exists. * @note This function returns -ENOSPC if there is insufficient space. */ int luksmeta_init(struct crypt_device *cd); /** * Gets metadata from the specified slot * * If buf is NULL, this function returns the size of the buffer needed and * the uuid. * * @param cd crypt device handle * @param slot requested metadata slot * @param uuid the UUID of the metadata (output) * @param buf output buffer for metadata (output) * @param size size of buf * @return The number of bytes in the metadata or negative errno value. * * @note This function returns -ENOENT if the device has no luksmeta header. * @note This function returns -EINVAL if the header or slot data is corrupted. * @note This function returns -EBADSLT if the specified slot is invalid. * @note This function returns -ENODATA if the specified slot is empty. * @note This function returns -E2BIG if the output buffer is too small. */ int luksmeta_load(struct crypt_device *cd, int slot, luksmeta_uuid_t uuid, void *buf, size_t size); /** * Sets metadata to the specified slot * * The slot parameter may be CRYPT_ANY_SLOT. * * @param cd crypt device handle * @param slot requested metadata slot * @param uuid UUID of the metadata * @param buf input buffer for metadata * @param size size of buf * @return The slot number to which data was written or negative errno value. * * @note This function returns -ENOENT if the device has no luksmeta header. * @note This function returns -EINVAL if the header is corrupted. * @note This function returns -EBADSLT if the specified slot is invalid. * @note This function returns -EKEYREJECTED if the uuid is invalid/reserved. * @note This function returns -EALREADY if the specified slot is not empty. * @note This function returns -ENOSPC if there is insufficient space. */ int luksmeta_save(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid, const void *buf, size_t size); /** * Deletes metadata from the specified slot * * If uuid is not NULL, this function will confirm that the specified slot * has a matching UUID before deletion. * * @param cd crypt device handle * @param slot requested metadata slot * @param uuid expected UUID (optional) * @return Zero on success or negative errno value otherwise. * * @note This function returns -ENOENT if the device has no luksmeta header. * @note This function returns -EINVAL if the header is corrupted. * @note This function returns -EBADSLT if the specified slot is invalid. * @note This function returns -EKEYREJECTED if the uuid doesn't match. * @note This function returns -EALREADY if the specified slot is empty. */ int luksmeta_wipe(struct crypt_device *cd, int slot, const luksmeta_uuid_t uuid); #ifdef __cplusplus } #endif latchset-luksmeta-9e93e21/luksmeta.pc.in000066400000000000000000000004051510111145300202540ustar00rootroot00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=${prefix}/include Name: LUKS Meta Description: Library for storing metadata in the LUKS header Version: @VERSION@ Requires: libcryptsetup Libs: -L${libdir} -lluksmeta Cflags: -I${includedir} latchset-luksmeta-9e93e21/test-crc32c.c000066400000000000000000000020521510111145300176760ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "crc32c.h" #include int main(int argc, char *argv[]) { char test[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; return crc32c(0, test, sizeof(test)) == 0xe3069283 ? 0 : 1; } latchset-luksmeta-9e93e21/test-lm-assumptions.c000066400000000000000000000023011510111145300216070ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "test.h" #include int main(int argc, char *argv[]) { crypt_free(test_format()); /* Test the layout state. */ assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { 0, 0 }, })); unlink(filename); return 0; } latchset-luksmeta-9e93e21/test-lm-big.c000066400000000000000000000161341510111145300177740ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "test.h" #include #include #include #include static const luksmeta_uuid_t UUID = { 0xd3, 0xd5, 0xf1, 0xe0, 0xa4, 0x6c, 0xbd, 0xf4, 0xca, 0x20, 0x30, 0x1f, 0xdd, 0xa0, 0xf7, 0xa8 }; static const uint8_t DATA[] = "DATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA" "DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA"; int main(int argc, char *argv[]) { uint8_t data[sizeof(DATA)] = {}; struct crypt_device *cd = NULL; luksmeta_uuid_t uuid = {}; uint32_t offset = 0; uint32_t length = 0; int r; crypt_free(test_format()); cd = test_init(); test_hole(cd, &offset, &length); /* Set the data. */ r = luksmeta_save(cd, CRYPT_ANY_SLOT, UUID, DATA, sizeof(DATA)); if (r < 0) error(EXIT_FAILURE, -r, "luksmeta_save()"); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ { offset + 4096, 4096 }, /* luksmeta slot 0 */ { offset + 8192, 4096 }, /* luksmeta slot 0 (cont) */ END(offset + 12288), /* Rest of the file */ })); assert(luksmeta_load(cd, r, uuid, data, sizeof(data)) == sizeof(data)); assert(memcmp(uuid, UUID, sizeof(UUID)) == 0); assert(memcmp(data, DATA, sizeof(DATA)) == 0); /* Delete the data. */ assert(luksmeta_wipe(cd, r, UUID) == 0); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ END(offset + 4096), /* Rest of the file */ })); crypt_free(cd); unlink(filename); return 0; } latchset-luksmeta-9e93e21/test-lm-init.c000066400000000000000000000105721510111145300201760ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "test.h" #include #include #include #include #include #include #include static const luksmeta_uuid_t UUID = { 0xfb, 0x06, 0x8c, 0x1f, 0x68, 0x04, 0x87, 0x9f, 0xf4, 0xcd, 0x32, 0x25, 0xb2, 0x1a, 0x7e, 0xf8 }; int main(int argc, char *argv[]) { uint8_t data[sizeof(UUID)] = {}; struct crypt_device *cd = NULL; luksmeta_uuid_t uuid = {}; uint32_t offset = 0; uint32_t length = 0; int fd; /* Test for -ENOENT when there is no luksmeta header. */ cd = test_format(); for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) { assert(luksmeta_save(cd, slot, UUID, UUID, sizeof(UUID)) == -ENOENT); assert(luksmeta_load(cd, slot, uuid, data, sizeof(data)) == -ENOENT); assert(luksmeta_wipe(cd, slot, UUID) == -ENOENT); assert(luksmeta_test(cd) == -ENOENT); } crypt_free(cd); cd = test_init(); test_hole(cd, &offset, &length); /* Test the layout state. */ assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ END(offset + 4096), /* Rest of the file */ })); /* Test error codes for a valid luksmeta header but no slot. */ for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) { assert(luksmeta_load(cd, slot, uuid, data, sizeof(data)) == -ENODATA); assert(luksmeta_wipe(cd, slot, UUID) == -EALREADY); } /* Test for -EALREADY when a valid header is present. */ assert(luksmeta_init(cd) == -EALREADY); assert(luksmeta_test(cd) == 0); /* Test for -EBADSLT when an invalid slot is used. */ assert(luksmeta_save(cd, 10, UUID, UUID, sizeof(UUID)) == -EBADSLT); assert(luksmeta_load(cd, 10, uuid, data, sizeof(data)) == -EBADSLT); assert(luksmeta_wipe(cd, 10, UUID) == -EBADSLT); assert(luksmeta_save(cd, -10, UUID, UUID, sizeof(UUID)) == -EBADSLT); assert(luksmeta_load(cd, -10, uuid, data, sizeof(data)) == -EBADSLT); assert(luksmeta_wipe(cd, -10, UUID) == -EBADSLT); assert(luksmeta_load(cd, -1, uuid, data, sizeof(data)) == -EBADSLT); assert(luksmeta_wipe(cd, -1, UUID) == -EBADSLT); /* Test for -EKEYREJECTED when a reserved UUID is used. */ assert(luksmeta_save(cd, CRYPT_ANY_SLOT, (luksmeta_uuid_t) {}, UUID, sizeof(UUID)) == -EKEYREJECTED); /* Test to make sure that data corruption is picked up correctly. */ fd = open(filename, O_RDWR | O_SYNC); if (fd < 0) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); if (lseek(fd, offset + 16, SEEK_SET) == -1) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); if (write(fd, &(char) { 17 }, 1) != 1) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); close(fd); assert(luksmeta_save(cd, 2, UUID, UUID, sizeof(UUID)) == -EINVAL); assert(luksmeta_load(cd, 2, uuid, data, sizeof(data)) == -EINVAL); assert(luksmeta_wipe(cd, 2, UUID) == -EINVAL); /* Test nuking */ assert(luksmeta_init(cd) == 0); assert(luksmeta_test(cd) == 0); assert(luksmeta_nuke(cd) == 0); assert(luksmeta_test(cd) == -ENOENT); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ END(offset), /* Rest of the file */ })); crypt_free(cd); unlink(filename); return 0; } latchset-luksmeta-9e93e21/test-lm-one.c000066400000000000000000000054551510111145300200200ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "test.h" #include #include #include #include static const luksmeta_uuid_t UUID = { 0xb0, 0xcc, 0xe0, 0x48, 0x89, 0x32, 0x90, 0x7e, 0x10, 0xd9, 0x62, 0xa2, 0x09, 0x3c, 0x05, 0x7d }; int main(int argc, char *argv[]) { uint8_t data[sizeof(UUID)] = {}; struct crypt_device *cd = NULL; luksmeta_uuid_t uuid = {}; uint32_t offset = 0; uint32_t length = 0; int r; crypt_free(test_format()); cd = test_init(); test_hole(cd, &offset, &length); r = luksmeta_save(cd, CRYPT_ANY_SLOT, UUID, UUID, sizeof(UUID)); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); /* Test the layout state. */ assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ { offset + 4096, 4096 }, /* luksmeta slot 0 */ END(offset + 8192), /* Rest of the file */ })); assert(luksmeta_load(cd, r, uuid, data, sizeof(data)) == sizeof(data)); assert(memcmp(uuid, UUID, sizeof(UUID)) == 0); assert(memcmp(data, UUID, sizeof(UUID)) == 0); assert(luksmeta_save(cd, r, UUID, UUID, sizeof(UUID)) == -EALREADY); assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) {}) == -EKEYREJECTED); assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) { 1 }) == -EKEYREJECTED); assert(luksmeta_wipe(cd, r, UUID) == 0); assert(luksmeta_wipe(cd, r, UUID) == -EALREADY); assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) {}) == -EALREADY); /* Test the layout state. */ assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ END(offset + 4096), /* Rest of the file */ })); crypt_free(cd); unlink(filename); return 0; } latchset-luksmeta-9e93e21/test-lm-two.c000066400000000000000000000077251510111145300200520ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "test.h" #include #include #include static const luksmeta_uuid_t UUID0 = { 0x35, 0x08, 0x50, 0xc3, 0x25, 0xc9, 0x85, 0xea, 0x1b, 0x55, 0x93, 0x56, 0x36, 0x2a, 0xd9, 0x85 }; static const luksmeta_uuid_t UUID1 = { 0xb4, 0xcb, 0x8c, 0x1c, 0x34, 0xea, 0xcc, 0x21, 0x0b, 0x9c, 0xc3, 0x9c, 0x9a, 0x09, 0xc0, 0x0f }; int main(int argc, char *argv[]) { uint8_t data[sizeof(UUID0)] = {}; struct crypt_device *cd = NULL; luksmeta_uuid_t uuid = {}; uint32_t offset = 0; uint32_t length = 0; int r; crypt_free(test_format()); cd = test_init(); test_hole(cd, &offset, &length); /* Add one metadata. */ r = luksmeta_save(cd, 0, UUID0, UUID0, sizeof(UUID0)); if (r < 0) error(EXIT_FAILURE, -r, "luksmeta_save()"); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ { offset + 4096, 4096 }, /* luksmeta slot 0 */ END(offset + 8192), /* Rest of the file */ })); assert(luksmeta_load(cd, 0, uuid, data, sizeof(data)) == sizeof(data)); assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0); assert(memcmp(data, UUID0, sizeof(UUID0)) == 0); /* Add a second metadata. */ r = luksmeta_save(cd, 1, UUID1, UUID1, sizeof(UUID1)); if (r < 0) error(EXIT_FAILURE, -r, "luksmeta_save()"); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ { offset + 4096, 4096 }, /* luksmeta slot 0 */ { offset + 8192, 4096 }, /* luksmeta slot 1 */ END(offset + 12288), /* Rest of the file */ })); assert(luksmeta_load(cd, 0, uuid, data, sizeof(data)) == sizeof(data)); assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0); assert(memcmp(data, UUID0, sizeof(UUID0)) == 0); assert(luksmeta_load(cd, 1, uuid, data, sizeof(data)) == sizeof(data)); assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0); assert(memcmp(data, UUID1, sizeof(UUID1)) == 0); /* Delete the first metadata. */ assert(luksmeta_wipe(cd, 0, UUID0) == 0); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ { offset + 4096, 4096, true }, /* luksmeta slot 0 */ { offset + 8192, 4096 }, /* luksmeta slot 1 */ END(offset + 12288), /* Rest of the file */ })); /* Delete the second metadata. */ assert(luksmeta_wipe(cd, 1, UUID1) == 0); assert(test_layout((range_t[]) { { 0, 1024 }, /* LUKS header */ { 1024, 3072, true }, /* Keyslot Area */ { offset, 4096 }, /* luksmeta header */ END(offset + 4096), /* Rest of the file */ })); crypt_free(cd); unlink(filename); return 0; } latchset-luksmeta-9e93e21/test-luksmeta000077500000000000000000000044271510111145300202360ustar00rootroot00000000000000#!/bin/bash -x trap 'exit' ERR export tmp=`mktemp /tmp/luksmeta.XXXXXXXXXX` export tmpdata=`mktemp /tmp/luksmeta.XXXXXXXXXX` function onexit() { rm -f $tmp rm -f "${tmpdata}" } trap 'onexit' EXIT truncate -s 4M $tmp echo -n foo | cryptsetup luksFormat --type luks1 $tmp - ! ./luksmeta test -d $tmp ./luksmeta init -f -d $tmp ./luksmeta test -d $tmp ./luksmeta init -f -d $tmp for slot in `seq 0 7`; do test "`./luksmeta show -s $slot -d $tmp`" == "" echo hi | ./luksmeta save -s $slot -u 23149359-1b61-4803-b818-774ab730fbec -d $tmp test "`./luksmeta show -s $slot -d $tmp`" == "23149359-1b61-4803-b818-774ab730fbec" test "`./luksmeta load -s $slot -d $tmp`" == "hi" test "`./luksmeta load -s $slot -u 23149359-1b61-4803-b818-774ab730fbec -d $tmp`" == "hi" ! ./luksmeta load -s $slot -u 23149359-1b61-4803-b818-774ab730fbed -d $tmp ! ./luksmeta wipe -f -s $slot -u 23149359-1b61-4803-b818-774ab730fbed -d $tmp ./luksmeta wipe -f -s $slot -u 23149359-1b61-4803-b818-774ab730fbec -d $tmp ! ./luksmeta load -s $slot -d $tmp echo hi | ./luksmeta save -s $slot -u 23149359-1b61-4803-b818-774ab730fbec -d $tmp ./luksmeta wipe -f -s $slot -d $tmp ! ./luksmeta load -s $slot -d $tmp done # Test nuking ./luksmeta test -d $tmp ./luksmeta init -f -d $tmp ./luksmeta nuke -f -d $tmp ! ./luksmeta test -d $tmp # Test implicit nuking ./luksmeta init -f -d $tmp echo hi | ./luksmeta save -s 0 -u 23149359-1b61-4803-b818-774ab730fbec -d $tmp test "`./luksmeta load -s 0 -d $tmp`" == "hi" ./luksmeta init -n -f -d $tmp ! ./luksmeta load -s 0 -d $tmp # CVE-2025-11568 - test attempt to store extremely large amount of data in a slot. ./luksmeta init -f -d "${tmp}" dd bs=1024k count=1 "${tmpdata}" ! ./luksmeta save -s 1 -u 23149359-1b61-4803-b818-774ab730fbec -d "${tmp}" < "${tmpdata}" # Additional test for CVE-2025-11568 boundary conditions. # Verify overflow protection with multiple existing slots at various offsets. ./luksmeta init -f -d "${tmp}" echo "a" | ./luksmeta save -s 0 -u 11111111-1111-1111-1111-111111111111 -d "${tmp}" echo "b" | ./luksmeta save -s 1 -u 22222222-2222-2222-2222-222222222222 -d "${tmp}" dd bs=1024 count=900 "${tmpdata}" ! ./luksmeta save -s 2 -u 33333333-3333-3333-3333-333333333333 -d "${tmp}" < "${tmpdata}" latchset-luksmeta-9e93e21/test.c000066400000000000000000000106601510111145300166250ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include "test.h" char filename[] = "/tmp/luksmetaXXXXXX"; static size_t first_nonzero(FILE *file, size_t start, size_t length) { uint8_t *buffer; buffer = malloc(length); if (!buffer) return false; if (fseek(file, start, SEEK_SET) != 0) { free(buffer); return false; } if (fread(buffer, length, 1, file) != 1) { free(buffer); return false; } for (size_t i = 0; i < length; i++) { if (buffer[i] != 0) { free(buffer); return i; } } free(buffer); return length; } bool test_layout(range_t *ranges) { bool valid = false; FILE *file = NULL; file = fopen(filename, "r"); if (!file) return false; for (size_t i = 0; ranges[i].length != 0; i++) { size_t nonzero; fprintf(stderr, "%08zu:%08zu (%c= 0)\n", ranges[i].start, ranges[i].start + ranges[i].length, ranges[i].zero ? '=' : '!'); nonzero = first_nonzero(file, ranges[i].start, ranges[i].length); if (ranges[i].zero && nonzero < ranges[i].length) { fprintf(stderr, "unexpected nonzero: %zu\n", nonzero); goto egress; } else if (!ranges[i].zero && nonzero == ranges[i].length) { fprintf(stderr, "unexpected zero: %zu-%zu\n", ranges[i].start, ranges[i].start + ranges[i].length); goto egress; } } valid = true; egress: fclose(file); return valid; } void test_hole(struct crypt_device *cd, uint32_t *offset, uint32_t *length) { uint64_t payload_offset = 0; uint64_t keyarea_end = 0; int r = 0; payload_offset = crypt_get_data_offset(cd) * 512; if (payload_offset < ALIGN(1, true)) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) { uint64_t off = 0; uint64_t len = 0; r = crypt_keyslot_area(cd, slot, &off, &len); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); if (off + len > keyarea_end) keyarea_end = off + len; } *offset = ALIGN(keyarea_end, true); *length = ALIGN(payload_offset, false) - *offset; } struct crypt_device * test_format(void) { struct crypt_device *cd = NULL; int fd; int r; fd = mkstemp(filename); if (fd < 0) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); /* Create a 4MB sparse file. */ if (lseek(fd, 4194303, SEEK_SET) == -1) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); if (write(fd, "", 1) != 1) error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__); close(fd); r = crypt_init(&cd, filename); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); r = crypt_format(cd, CRYPT_LUKS1, "aes", "xts-plain64", NULL, NULL, 32, NULL); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); return cd; } struct crypt_device * test_init(void) { struct crypt_device *cd = NULL; int r; r = crypt_init(&cd, filename); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); r = crypt_load(cd, CRYPT_LUKS1, NULL); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); r = luksmeta_init(cd); if (r < 0) error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__); return cd; } latchset-luksmeta-9e93e21/test.h000066400000000000000000000026431510111145300166340ustar00rootroot00000000000000/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */ /* * Copyright (c) 2016 Red Hat, Inc. * Author: Nathaniel McCallum * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "luksmeta.h" #include #include /* All tests need assert() */ #include /* All tests need unlink() */ #define FILESIZE 4194304 #define END(s) { (s), FILESIZE - (s), true }, { 0, 0 } #define ALIGN(s, up) (((s) + (up ? 4095 : 0)) & ~4095ULL) typedef struct { size_t start; size_t length; bool zero; } range_t; extern char filename[]; bool test_layout(range_t *ranges); void test_hole(struct crypt_device *cd, uint32_t *offset, uint32_t *length); struct crypt_device * test_format(void); struct crypt_device * test_init(void);