pax_global_header00006660000000000000000000000064146053273610014521gustar00rootroot0000000000000052 comment=f5d39702622208b3ada064d7b2eaeaf1454c9bd3 libcap-ng-0.8.5/000077500000000000000000000000001460532736100133675ustar00rootroot00000000000000libcap-ng-0.8.5/.github/000077500000000000000000000000001460532736100147275ustar00rootroot00000000000000libcap-ng-0.8.5/.github/workflows/000077500000000000000000000000001460532736100167645ustar00rootroot00000000000000libcap-ng-0.8.5/.github/workflows/codeql-analysis.yml000066400000000000000000000047421460532736100226060ustar00rootroot00000000000000# For most projects, this workflow file will not need changing; you simply need # to commit it to your repository. # # You may wish to alter this file to override the set of languages analyzed, # or to provide custom queries or build logic. # # ******** NOTE ******** # We have attempted to detect the languages in your repository. Please check # the `language` matrix defined below to confirm you have the correct set of # supported CodeQL languages. # name: "CodeQL" on: push: branches: [ master ] pull_request: # The branches below must be a subset of the branches above branches: [ master ] schedule: - cron: '35 12 * * 4' jobs: analyze: name: Analyze runs-on: ubuntu-latest container: registry.fedoraproject.org/fedora:latest strategy: fail-fast: false matrix: language: [ 'cpp', 'python' ] # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] # Learn more: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - name: Checkout repository uses: actions/checkout@v2 - name: get envinronment info run: uname -a - name: print fedora version run: cat /etc/fedora-release - name: install dependencies run: dnf -y install autoconf automake libtool gcc libattr-devel kernel-headers swig python3-devel python3 python-unversioned-command which git # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v1 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main # ℹ️ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines # and modify them (or add more) to build your code if your project # uses a compiled language - run: | ./autogen.sh ./configure --with-python=no --with-python3 make - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v1 libcap-ng-0.8.5/AUTHORS000066400000000000000000000001031460532736100144310ustar00rootroot00000000000000This library has been written by: Steve Grubb libcap-ng-0.8.5/COPYING000066400000000000000000000431101460532736100144210ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, 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 or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's 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 give any other recipients of the Program a copy of this License along with the Program. 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 Program or any portion of it, thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, 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 Program, 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 Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) 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; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, 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 executable. However, as a special exception, the source code 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. If distribution of executable or 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 counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program 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. 5. 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 Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program 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 to this License. 7. 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 Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program 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 Program. 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. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program 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. 9. The Free Software Foundation may publish revised and/or new versions of the 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 Program 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 Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, 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 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "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 PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 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 Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. 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 program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. libcap-ng-0.8.5/COPYING.LIB000066400000000000000000000636561460532736100150470ustar00rootroot00000000000000 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. ^L 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 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. ^L 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 General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. ^L Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU 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. ^L 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. ^L 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. ^L 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. ^L 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 ^L 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 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! libcap-ng-0.8.5/ChangeLog000066400000000000000000000137641460532736100151540ustar00rootroot000000000000000.8.5 - Remove python global exception handler since it's deprecated - Make the utilities link against just built libraries - Remove unused macro in cap-ng.h 0.8.4 - In capng_change_id, clear PR_SET_KEEPCAPS if returning an error - pscap: add -p option for reporting a specified process (Masatake Yamato) - Annotate function prototypes to warn if results are unused - Drop python2 support 0.8.3 - Fix parameters to capng_updatev python bindings to be signed - Detect capability options at runtime to make containerization easier (ntkme) - Initialize the library when linked statically - Add gcc function attributes for deallocation 0.8.2 - In capng_apply, if we blew up in bounding set, allow setting capabilities - If PR_CAP_AMBIENT is not available, do not build libdrop_ambient - Improve last_cap check 0.8.1 - If procfs is not available, leave last_cap as CAP_LAST_CAP - If bounding and ambient not found in status, try prctl method - In capng_apply, move ambient caps to the end of the transaction - In capng_apply, return errors more aggressively. - In capng_apply, if the action includes the bounding set,resync with the kernel - Fix signed/unsigned warning in cap-ng.c - In capng_apply, return a unique error code to diagnose any failure - In capng_have_capability, return 0 for failure - Add the libdrop_ambient admin tool 0.8 - Add vararg support to python bindings for capng_updatev - Add support for ambient capabilities - Add support for V3 filesystem capabilities 0.7.11 - Really clear bounding set if asked in capng_change_id - Add CAP_PERFMON, CAP_BPF, & CAP_CHECKPOINT_RESTORE - Avoid malloc/free in capng_apply (Natanael Copa) - If procfs is not available, get bounding set via prctl - Cleanup some compiler warnings 0.7.10 - Update capng_change_id man page - Add capng_have_permitted_capabilities function - Update filecap to output which set the capabilities are in - Fix filecap to not output an error when a file has no capabilities - Add udplite support to netcap - Fix usage of pthread_atfork (Joe Orton) - Mark processes in child user namespaces with * (Danila Kiver) 0.7.9 - Fix byte compiling python3 bindings - Detect and output a couple errors in filecap - Use pthread_atfork to optionally reset the pid and related info on fork - Rework spec file to show new python2/3 separation 0.7.8 - Improve Python3 support - Fix the thread separation test - Correct typo in cap_pacct text - Update man page for captest - Fix sscanf string lengths in netcap - Correct linking of python3 module 0.7.7 - Make sure all types used in _lnode are defined in proc-llist.h - Fix python binding test for old kernels - Fix leaked FD in library init 0.7.6 - Fix python3 support 0.7.5 - Make python3 supported - In python bindings test, clamp CAP_LAST_CAP with /proc/.../cap_last_cap - Update table for 3.16 kernel 0.7.4 - In pscap, remove unused code - Add CAPNG_INIT_SUPP_GRP to capng_change_id - Drop CAP_COMPROMISE_KERNEL - Update the autotools components - Dynamically detect last capability (#895105) - Add PR_SET_NO_NEW_PRIVS to capng_lock if kernel supports it 0.7.3 - Make sure stderr is used consistently in utils - Fix logic causing file based capabilities to not be supported when it should 0.7.1 - Add CAP_COMPROMISE_KERNEL - Define FTW_CONTINUE in case its not defined in libc - Use glibc for xattr.h if available 0.7 - Make file opens use the cloexec flag (Cristian Rodríguez) - Add CAP_BLOCK_SUSPEND - Fix possible segfaults when CAP_LAST_CAP is larger than the lookup table - In pscap, don't drop capabilities when running with capabilities 0.6.6 - In netcap, make sure readlink is handled properly - Add CAP_SYSLOG - In netcap and pscap, ensure euid is initialized - Add CAP_WAKE_ALARM 0.6.5 - Fix self test build problem on clean system (Sterling X. Winter) - Only open regular files in filecap - Make building Python bindings optional - Python bindings update (arfrever.fta) - Fix filecap segfault when checking a specific file - Add define for missing XATTR_NAME_CAPS since 2.6.36 makes it private 0.6.4 - Update packet socket code to print interface - Fix effective capabilities read from file descriptor - Use thread ID for capget/set calls 0.6.3 - In netcap and pscap use the effective uid - In capng_change_id, only retain setpcap if clearing the bounding set 0.6.2 - Make pscap drop capabilities so its not listed in report - Review prctl calls to make sure we are passing 5 args - Add package config support 0.6.1 - In netcap, don't complain about missing udp or raw network files - Adjusted data read in for file based capabilities 0.6 - In netcap, don't complain about missing network files - Add python bindings - Add m4 macro file to help developers configure libcap-ng in their apps - Fake applying bounding set for old OS - Ignore setpcap for old OS when changing id - Remove capabilities v1 data handling from reading file attributes - Set the SECURE_NO_SETUID_FIXUP and LOCKED securebits flags in capng_lock 0.5.1 - Remove unnecessary uid check in change_uid when dropping supplemental groups - Add credential printout and other improvements to captest - In the init routine, set hdr.pid to current process - Use bit mask on effective capabilities check in have_capabilities - Numeric printing of bounding set bits were in wrong order - In update function, reverse the order of bounding set vs capabilities - Revise the tests used to determine if bounding set should be updated 0.5 - If attr/xattr.h is not available disable file system capabilities - Initialize capng_have_capability with capng_get_caps_process if unknown - Make capng_change_id drop the gid if given - Fixed cap_update for bounding set - Fix have_capability for bounding set - Added more tests to the make check target - Remove CAPNG_LOCK_PERMS for change_id flags - Added captest program 0.4.2 - Fix missing includes for various OS and platforms - Correct misplaced #ifdef for older OS - Reorder clearing of bounding set in capng_change_id - Make locking a noop in capng_change_id for the moment 0.4.1 - spec file clean ups - Man pages for all library functions 0.4 - Initial public release libcap-ng-0.8.5/INSTALL000066400000000000000000000363321460532736100144270ustar00rootroot00000000000000Installation Instructions ************************* Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without warranty of any kind. Basic Installation ================== Briefly, the shell commands `./configure; make; make install' should configure, build, and install this package. The following more-detailed instructions are generic; see the `README' file for instructions specific to this package. Some packages provide this `INSTALL' file but do not implement all of the features documented below. The lack of an optional feature in a given package is not necessarily a bug. More recommendations for GNU packages can be found in *note Makefile Conventions: (standards)Makefile Conventions. The `configure' shell script attempts to guess correct values for various system-dependent variables used during compilation. It uses those values to create a `Makefile' in each directory of the package. It may also create one or more `.h' files containing system-dependent definitions. Finally, it creates a shell script `config.status' that you can run in the future to recreate the current configuration, and a file `config.log' containing compiler output (useful mainly for debugging `configure'). It can also use an optional file (typically called `config.cache' and enabled with `--cache-file=config.cache' or simply `-C') that saves the results of its tests to speed up reconfiguring. Caching is disabled by default to prevent problems with accidental use of stale cache files. If you need to do unusual things to compile the package, please try to figure out how `configure' could check whether to do them, and mail diffs or instructions to the address given in the `README' so they can be considered for the next release. If you are using the cache, and at some point `config.cache' contains results you don't want to keep, you may remove or edit it. The file `configure.ac' (or `configure.in') is used to create `configure' by a program called `autoconf'. You need `configure.ac' if you want to change it or regenerate `configure' using a newer version of `autoconf'. The simplest way to compile this package is: 1. `cd' to the directory containing the package's source code and type `./configure' to configure the package for your system. Running `configure' might take a while. While running, it prints some messages telling which features it is checking for. 2. Type `make' to compile the package. 3. Optionally, type `make check' to run any self-tests that come with the package, generally using the just-built uninstalled binaries. 4. Type `make install' to install the programs and any data files and documentation. When installing into a prefix owned by root, it is recommended that the package be configured and built as a regular user, and only the `make install' phase executed with root privileges. 5. Optionally, type `make installcheck' to repeat any self-tests, but this time using the binaries in their final installed location. This target does not install anything. Running this target as a regular user, particularly if the prior `make install' required root privileges, verifies that the installation completed correctly. 6. You can remove the program binaries and object files from the source code directory by typing `make clean'. To also remove the files that `configure' created (so you can compile the package for a different kind of computer), type `make distclean'. There is also a `make maintainer-clean' target, but that is intended mainly for the package's developers. If you use it, you may have to get all sorts of other programs in order to regenerate files that came with the distribution. 7. Often, you can also type `make uninstall' to remove the installed files again. In practice, not all packages have tested that uninstallation works correctly, even though it is required by the GNU Coding Standards. 8. Some packages, particularly those that use Automake, provide `make distcheck', which can by used by developers to test that all other targets like `make install' and `make uninstall' work correctly. This target is generally not run by end users. Compilers and Options ===================== Some systems require unusual options for compilation or linking that the `configure' script does not know about. Run `./configure --help' for details on some of the pertinent environment variables. You can give `configure' initial values for configuration parameters by setting variables in the command line or in the environment. Here is an example: ./configure CC=c99 CFLAGS=-g LIBS=-lposix *Note Defining Variables::, for more details. Compiling For Multiple Architectures ==================================== You can compile the package for more than one kind of computer at the same time, by placing the object files for each architecture in their own directory. To do this, you can use GNU `make'. `cd' to the directory where you want the object files and executables to go and run the `configure' script. `configure' automatically checks for the source code in the directory that `configure' is in and in `..'. This is known as a "VPATH" build. With a non-GNU `make', it is safer to compile the package for one architecture at a time in the source code directory. After you have installed the package for one architecture, use `make distclean' before reconfiguring for another architecture. On MacOS X 10.5 and later systems, you can create libraries and executables that work on multiple system types--known as "fat" or "universal" binaries--by specifying multiple `-arch' options to the compiler but only a single `-arch' option to the preprocessor. Like this: ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ CPP="gcc -E" CXXCPP="g++ -E" This is not guaranteed to produce working output in all cases, you may have to build one architecture at a time and combine the results using the `lipo' tool if you have problems. Installation Names ================== By default, `make install' installs the package's commands under `/usr/local/bin', include files under `/usr/local/include', etc. You can specify an installation prefix other than `/usr/local' by giving `configure' the option `--prefix=PREFIX', where PREFIX must be an absolute file name. You can specify separate installation prefixes for architecture-specific files and architecture-independent files. If you pass the option `--exec-prefix=PREFIX' to `configure', the package uses PREFIX as the prefix for installing programs and libraries. Documentation and other data files still use the regular prefix. In addition, if you use an unusual directory layout you can give options like `--bindir=DIR' to specify different values for particular kinds of files. Run `configure --help' for a list of the directories you can set and what kinds of files go in them. In general, the default for these options is expressed in terms of `${prefix}', so that specifying just `--prefix' will affect all of the other directory specifications that were not explicitly provided. The most portable way to affect installation locations is to pass the correct locations to `configure'; however, many packages provide one or both of the following shortcuts of passing variable assignments to the `make install' command line to change installation locations without having to reconfigure or recompile. The first method involves providing an override variable for each affected directory. For example, `make install prefix=/alternate/directory' will choose an alternate location for all directory configuration variables that were expressed in terms of `${prefix}'. Any directories that were specified during `configure', but not in terms of `${prefix}', must each be overridden at install time for the entire installation to be relocated. The approach of makefile variable overrides for each directory variable is required by the GNU Coding Standards, and ideally causes no recompilation. However, some platforms have known limitations with the semantics of shared libraries that end up requiring recompilation when using this method, particularly noticeable in packages that use GNU Libtool. The second method involves providing the `DESTDIR' variable. For example, `make install DESTDIR=/alternate/directory' will prepend `/alternate/directory' before all installation names. The approach of `DESTDIR' overrides is not required by the GNU Coding Standards, and does not work on platforms that have drive letters. On the other hand, it does better at avoiding recompilation issues, and works well even when some directory options were not specified in terms of `${prefix}' at `configure' time. Optional Features ================= If the package supports it, you can cause programs to be installed with an extra prefix or suffix on their names by giving `configure' the option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. Some packages pay attention to `--enable-FEATURE' options to `configure', where FEATURE indicates an optional part of the package. They may also pay attention to `--with-PACKAGE' options, where PACKAGE is something like `gnu-as' or `x' (for the X Window System). The `README' should mention any `--enable-' and `--with-' options that the package recognizes. For packages that use the X Window System, `configure' can usually find the X include and library files automatically, but if it doesn't, you can use the `configure' options `--x-includes=DIR' and `--x-libraries=DIR' to specify their locations. Some packages offer the ability to configure how verbose the execution of `make' will be. For these packages, running `./configure --enable-silent-rules' sets the default to minimal output, which can be overridden with `make V=1'; while running `./configure --disable-silent-rules' sets the default to verbose, which can be overridden with `make V=0'. Particular systems ================== On HP-UX, the default C compiler is not ANSI C compatible. If GNU CC is not installed, it is recommended to use the following options in order to use an ANSI C compiler: ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" and if that doesn't work, install pre-built binaries of GCC for HP-UX. On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot parse its `' header file. The option `-nodtk' can be used as a workaround. If GNU CC is not installed, it is therefore recommended to try ./configure CC="cc" and if that doesn't work, try ./configure CC="cc -nodtk" On Solaris, don't put `/usr/ucb' early in your `PATH'. This directory contains several dysfunctional programs; working variants of these programs are available in `/usr/bin'. So, if you need `/usr/ucb' in your `PATH', put it _after_ `/usr/bin'. On Haiku, software installed for all users goes in `/boot/common', not `/usr/local'. It is recommended to use the following options: ./configure --prefix=/boot/common Specifying the System Type ========================== There may be some features `configure' cannot figure out automatically, but needs to determine by the type of machine the package will run on. Usually, assuming the package is built to be run on the _same_ architectures, `configure' can figure that out, but if it prints a message saying it cannot guess the machine type, give it the `--build=TYPE' option. TYPE can either be a short name for the system type, such as `sun4', or a canonical name which has the form: CPU-COMPANY-SYSTEM where SYSTEM can have one of these forms: OS KERNEL-OS See the file `config.sub' for the possible values of each field. If `config.sub' isn't included in this package, then this package doesn't need to know the machine type. If you are _building_ compiler tools for cross-compiling, you should use the option `--target=TYPE' to select the type of system they will produce code for. If you want to _use_ a cross compiler, that generates code for a platform different from the build platform, you should specify the "host" platform (i.e., that on which the generated programs will eventually be run) with `--host=TYPE'. Sharing Defaults ================ If you want to set default values for `configure' scripts to share, you can create a site shell script called `config.site' that gives default values for variables like `CC', `cache_file', and `prefix'. `configure' looks for `PREFIX/share/config.site' if it exists, then `PREFIX/etc/config.site' if it exists. Or, you can set the `CONFIG_SITE' environment variable to the location of the site script. A warning: not all `configure' scripts look for a site script. Defining Variables ================== Variables not defined in a site shell script can be set in the environment passed to `configure'. However, some packages may run configure again during the build, and the customized values of these variables may be lost. In order to avoid this problem, you should set them in the `configure' command line, using `VAR=value'. For example: ./configure CC=/usr/local2/bin/gcc causes the specified `gcc' to be used as the C compiler (unless it is overridden in the site shell script). Unfortunately, this technique does not work for `CONFIG_SHELL' due to an Autoconf bug. Until the bug is fixed you can use this workaround: CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash `configure' Invocation ====================== `configure' recognizes the following options to control how it operates. `--help' `-h' Print a summary of all of the options to `configure', and exit. `--help=short' `--help=recursive' Print a summary of the options unique to this package's `configure', and exit. The `short' variant lists options used only in the top level, while the `recursive' variant lists options also present in any nested packages. `--version' `-V' Print the version of Autoconf used to generate the `configure' script, and exit. `--cache-file=FILE' Enable the cache: use and save the results of the tests in FILE, traditionally `config.cache'. FILE defaults to `/dev/null' to disable caching. `--config-cache' `-C' Alias for `--cache-file=config.cache'. `--quiet' `--silent' `-q' Do not print messages saying which checks are being made. To suppress all normal output, redirect it to `/dev/null' (any error messages will still be shown). `--srcdir=DIR' Look for the package's source code in directory DIR. Usually `configure' can determine that directory automatically. `--prefix=DIR' Use DIR as the installation prefix. *note Installation Names:: for more details, including other options available for fine-tuning the installation locations. `--no-create' `-n' Run the configure checks, but stop before creating any output files. `configure' also accepts some other, not widely useful, options. Run `configure --help' for more details. libcap-ng-0.8.5/Makefile.am000066400000000000000000000022001460532736100154150ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009 Red Hat Inc. # All Rights Reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # ACLOCAL_AMFLAGS = -I m4 SUBDIRS = src utils m4 docs if HAVE_SWIG SUBDIRS += bindings endif EXTRA_DIST = ChangeLog AUTHORS README.md INSTALL COPYING COPYING.LIB \ libcap-ng.spec autogen.sh CONFIG_CLEAN_FILES = debug*.list config/* clean-generic: rm -rf autom4te*.cache rm -f *.rej *.orig *.lang libcap-ng-0.8.5/README.md000066400000000000000000000167031460532736100146550ustar00rootroot00000000000000libcap-ng ========= The libcap-ng library should make programming with POSIX capabilities easier. The library has some utilities to help you analyse a system for apps that may have too much privileges. The included utilities are designed to let admins and developers spot apps from various ways that may be running with too much privilege. For example, any investigation should start with network facing apps since they would be prime targets for intrusion. The netcap program will check all running apps that have listening socket and display the results. Sample output from netcap: ``` ppid pid acct command type port capabilities 1 2295 root nasd tcp 8000 full 2323 2383 root dnsmasq tcp 53 net_admin, net_raw + 1 2286 root sshd tcp 22 full 1 2365 root cupsd tcp 631 full 1 2286 root sshd tcp6 22 full 1 2365 root cupsd tcp6 631 full 2323 2383 root dnsmasq udp 53 net_admin, net_raw + 2323 2383 root dnsmasq udp 67 net_admin, net_raw + 1 2365 root cupsd udp 631 full ``` After checking the networking apps, you should check all running apps with pscap. If you are a developer and have to give your application CAP_DAC_OVERRIDE, you must be accessing files for which you have no permission to access. This typically can be resolved by having membership in the correct groups. Try to avoid needing CAP_DAC_OVERRIDE...you may as well be root if you need it. Some application developers have chosen to use file system base capabilities rather than be setuid root and have to drop capabilities. Libcap-ng provides filecap to recursively search directories and show you which ones have capabilities and exactly what those are. C Examples ---------- As an application developer, there are probably 6 use cases that you are interested in: drop all capabilities, keep one capability, keep several capabilities, check if you have any capabilities at all, check for certain capabilities, and retain capabilities across a uid change. 1) Drop all capabilities ```c capng_clear(CAPNG_SELECT_BOTH); capng_apply(CAPNG_SELECT_BOTH); ``` 2) Keep one capability ```c capng_clear(CAPNG_SELECT_BOTH); capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_CHOWN); capng_apply(CAPNG_SELECT_BOTH); ``` 3) Keep several capabilities ```c capng_clear(CAPNG_SELECT_BOTH); capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETUID, CAP_SETGID, -1); capng_apply(CAPNG_SELECT_BOTH); ``` 4) Check if you have any capabilities ```c if (capng_have_capabilities(CAPNG_SELECT_CAPS) > CAPNG_NONE) do_something(); ``` 5) Check for a specific capability ```c if (capng_have_capability(CAPNG_EFFECTIVE, CAP_CHOWN)) do_something(); ``` 6) Retain capabilities across a uid change ```c capng_clear(CAPNG_SELECT_BOTH); capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_CHOWN); if (capng_change_id(99, 99, CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)) error(); ``` Now, isn't that a lot simpler? Note that the last example takes about 60 lines of code using the older capabilities library. As of the 0.6 release, there is a m4 macro file to help adding libcap-ng to your autotools config system. In configure.ac, add LIBCAP_NG_PATH. Then in Makefile.am locate the apps that link to libcap-ng, add $(CAPNG_LDADD) to their LDADD entries. And lastly, surround the optional capabilities code with #ifdef HAVE_LIBCAP_NG. Python ------ Libcap-ng 0.6 and later has python bindings. (Only python3 is supported from 0.8.4 onward.) You simply add 'import capng' in your script. Here are the same examples as above in python: 1) Drop all capabilities ```python capng.capng_clear(capng.CAPNG_SELECT_BOTH) capng.capng_apply(capng.CAPNG_SELECT_BOTH) ``` 2) Keep one capability ```python capng.capng_clear(capng.CAPNG_SELECT_BOTH) capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE|capng.CAPNG_PERMITTED, capng.CAP_CHOWN) capng.capng_apply(capng.CAPNG_SELECT_BOTH) ``` 3) Keep several capabilities ```python capng.capng_clear(capng.CAPNG_SELECT_BOTH) capng.capng_updatev(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE|capng.CAPNG_PERMITTED, capng.CAP_SETUID, capng.CAP_SETGID, -1) capng.capng_apply(capng.CAPNG_SELECT_BOTH) ``` 4) Check if you have any capabilities ```python if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS) > capng.CAPNG_NONE: do_something() ``` 5) Check for a specific capability ```python if capng.capng_have_capability(capng.CAPNG_EFFECTIVE, capng.CAP_CHOWN): do_something() ``` 6) Retain capabilities across a uid change ```python capng.capng_clear(capng.CAPNG_SELECT_BOTH) capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE|capng.CAPNG_PERMITTED, capng.CAP_CHOWN) if capng.capng_change_id(99, 99, capng.CAPNG_DROP_SUPP_GRP | capng.CAPNG_CLEAR_BOUNDING) < 0: error() ``` The one caveat is that printing capabilities from python does not work. But you can still manipulate capabilities, though. Ambient Capabilities -------------------- Ambient capabilities arrived in the 4.3 Linux kernel. Ambient capabilities allow a privileged process to bestow capabilities to a child process. This is how systemd grants capabilities to a daemon running in a service account. The problem with ambient capabilities is they are inherited forever. Every process exec'ed from the original service also has the capabilities. This is a security issue. To find and fix this, you can run the pscap program and grep for '@'. The '@' symbol denotes processes that have ambient capabilities. For example: ``` # pscap | grep @ 1 1655 systemd-oom systemd-oomd dac_override, kill @ + 1 1656 systemd-resolve systemd-resolve net_raw @ + ``` To fix this, libcap-ng 0.8.3 and later ships libdrop_ambient.so.0. It is designed to be used with LD_PRELOAD. It has a constructor function that forces the dropping of ambient capabilities. By the time the application starts, it has both effective and ambient capabilities - meaning is safe to drop ambient capabilities very early. You can either link it to an application run as a systemd service (using ld), or create a wrapper script that then starts the daemon. Building -------- After cloning libcap-ng, run: ``` cd libcap-ng ./autogen.sh ./configure make make install ``` If you want python bindings, add that option to the configure command. There is also a spec file to use if you are on a rpm based distribution. To do that, run "make dist" instead of make in the above instructions. Then use the resulting tar file with the spec file. NOTE: to distributions ---------------------- There is a "make check" target. It only works if the available kernel headers roughly match the build root kernel. Iow, if you have a chroot build system that is using a much older kernel, the macros in the kernel header files will describe functionality that does not exist in the build root. The capng_init function will probe the kernel and decide we can only do v1 rather than v3 capabilities instead of what the kernel headers said was possible. If that is your case, just don't do the "make check" as part of the build process. This problem should go away as build roots eventually switch to the 5.0 or later kernels. Reporting --------- Report any bugs in this package to: https://github.com/stevegrubb/libcap-ng/issue libcap-ng-0.8.5/autogen.sh000077500000000000000000000001701460532736100153660ustar00rootroot00000000000000#! /bin/sh set -x -e # --no-recursive is available only in recent autoconf versions touch NEWS autoreconf -fv --install libcap-ng-0.8.5/bindings/000077500000000000000000000000001460532736100151645ustar00rootroot00000000000000libcap-ng-0.8.5/bindings/Makefile.am000066400000000000000000000017211460532736100172210ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,2015 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # CONFIG_CLEAN_FILES = *.loT *.rej *.orig SUBDIRS = src if USE_PYTHON3 SUBDIRS += python3 endif libcap-ng-0.8.5/bindings/python3/000077500000000000000000000000001460532736100165705ustar00rootroot00000000000000libcap-ng-0.8.5/bindings/python3/Makefile.am000066400000000000000000000036231460532736100206300ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,2014-17 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # SUBDIRS = test CONFIG_CLEAN_FILES = *.loT *.rej *.orig AM_CFLAGS = -fPIC -DPIC $(PYTHON3_CFLAGS) AM_CPPFLAGS = -I. -I$(top_builddir) $(PYTHON3_INCLUDES) LIBS = ${top_builddir}/src/libcap-ng.la SWIG_FLAGS = -python SWIG_INCLUDES = ${AM_CPPFLAGS} PYTHON = $(PYTHON3) pyexec_PYTHON = capng.py pyexec_LTLIBRARIES = _capng.la pyexec_SOLIBRARIES = _capng.so _capng_la_CFLAGS = -shared _capng_la_LDFLAGS = -module -avoid-version -Wl,-z,relro _capng_la_HEADERS: $(top_builddir)/config.h _capng_la_DEPENDENCIES =${top_srcdir}/src/cap-ng.h ${top_builddir}/src/libcap-ng.la _capng_la_LIBADD = ${top_builddir}/src/libcap-ng.la nodist__capng_la_SOURCES = capng_wrap.c capng.py capng_wrap.c: ${srcdir}/../src/capng_swig.i caps.h capng.h swig -o capng_wrap.c ${SWIG_FLAGS} ${SWIG_INCLUDES} ${srcdir}/../src/capng_swig.i caps.h: cat $(CAPABILITY_HEADER) | grep '^#define CAP' | grep -v '[()]' > caps.h capng.h: cat ${top_srcdir}/src/cap-ng.h | grep -v '_state' > capng.h CLEANFILES = capng.py* capng_wrap.c caps.h capng.h *~ clean-local: $(RM) -rf __pycache__ libcap-ng-0.8.5/bindings/python3/test/000077500000000000000000000000001460532736100175475ustar00rootroot00000000000000libcap-ng-0.8.5/bindings/python3/test/Makefile.am000066400000000000000000000020251460532736100216020ustar00rootroot00000000000000# Makefile.am -- # Copyright 2023 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # dist_check_SCRIPTS = capng-test.py TESTS = $(dist_check_SCRIPTS) TESTS_ENVIRONMENT = PYTHONPATH=${top_builddir}/bindings/python3/:${top_builddir}/bindings/python3/.libs libcap-ng-0.8.5/bindings/python3/test/capng-test.py000077500000000000000000000043171460532736100221760ustar00rootroot00000000000000#!/usr/bin/env python3 import os import sys import time load_path = '../' if False: sys.path.insert(0, load_path) import capng last = capng.CAP_LAST_CAP try: with open('/proc/sys/kernel/cap_last_cap', 'r') as f: last = int(f.readline()) except IOError as e: print("Error opening /proc/sys/kernel/cap_last_cap: {0}".format(e.strerror)) print("Doing basic bit tests...") capng.capng_clear(capng.CAPNG_SELECT_BOTH) if capng.capng_have_capabilities(capng.CAPNG_SELECT_BOTH) != capng.CAPNG_NONE: print("Failed clearing capabilities\n") sys.exit(1) capng.capng_fill(capng.CAPNG_SELECT_BOTH) if capng.capng_have_capabilities(capng.CAPNG_SELECT_BOTH) != capng.CAPNG_FULL: print("Failed filling capabilities") sys.exit(1) text = capng.capng_print_caps_numeric(capng.CAPNG_PRINT_BUFFER, capng.CAPNG_SELECT_CAPS) len = len(text) if len < 80 and last > 30: last = 30 print("Doing advanced bit tests for %d capabilities...\n" % (last)) for i in range(last+1): capng.capng_clear(capng.CAPNG_SELECT_BOTH) rc = capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE, i) if rc: print("Failed update test 1") sys.exit(1) rc = capng.capng_have_capability(capng.CAPNG_EFFECTIVE, int(i)) if rc <= capng.CAPNG_NONE: print("Failed have capability test 1") capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS) sys.exit(1) if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS) != capng.CAPNG_PARTIAL: print("Failed have capabilities test 1") sys.exit(1) capng.capng_fill(capng.CAPNG_SELECT_BOTH) rc = capng.capng_update(capng.CAPNG_DROP, capng.CAPNG_EFFECTIVE, i) if rc: print("Failed update test 3") sys.exit(1) if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS)!=capng.CAPNG_PARTIAL: print("Failed have capabilities test 3") capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS) sys.exit(1) rc = capng.capng_update(capng.CAPNG_ADD, capng.CAPNG_EFFECTIVE, i) if rc: print("Failed update test 4") sys.exit(1) if capng.capng_have_capabilities(capng.CAPNG_SELECT_CAPS) != capng.CAPNG_FULL: print("Failed have capabilities test 4") capng.capng_print_caps_numeric(capng.CAPNG_PRINT_STDOUT, capng.CAPNG_SELECT_CAPS) sys.exit(1) sys.exit(0) libcap-ng-0.8.5/bindings/src/000077500000000000000000000000001460532736100157535ustar00rootroot00000000000000libcap-ng-0.8.5/bindings/src/Makefile.am000066400000000000000000000017061460532736100200130ustar00rootroot00000000000000# Makefile.am -- # Copyright 2015 Red Hat Inc., Durham, North Carolina. # All Rights Reserved. # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Authors: # Steve Grubb # EXTRA_DIST = capng_swig.i SWIG_SOURCES = capng_swig.i CONFIG_CLEAN_FILES = *.loT *.rej *.orig libcap-ng-0.8.5/bindings/src/capng_swig.i000066400000000000000000000022621460532736100202500ustar00rootroot00000000000000/* capngswig.i -- * Copyright 2009 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ %module capng %{ #include "./capng.h" %} #if defined(SWIGPYTHON) %varargs(16, signed capability = 0) capng_updatev; #endif %define __signed__ signed %enddef #define __attribute(X) /*nothing*/ typedef unsigned __u32; #define __extension__ /*nothing*/ %include "./caps.h" %include "./capng.h" libcap-ng-0.8.5/configure.ac000066400000000000000000000162261460532736100156640ustar00rootroot00000000000000 dnl define([AC_INIT_NOTICE], [### Generated automatically using autoconf version] AC_ACVERSION [ ### Copyright 2009-2023 Steve Grubb ### ### Permission is hereby granted, free of charge, to any person obtaining a ### copy of this software and associated documentation files (the "Software"), ### to deal in the Software without restriction, including without limitation ### the rights to use, copy, modify, merge, publish, distribute, sublicense, ### and/or sell copies of the Software, and to permit persons to whom the ### Software is furnished to do so, subject to the following conditions: ### ### The above copyright notice and this permission notice shall be included ### in all copies or substantial portions of the Software. ### ### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ### THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR ### OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ### ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR ### OTHER DEALINGS IN THE SOFTWARE. ### ### For usage, run `./configure --help' ### For more detailed information on installation, read the file `INSTALL'. ### ### If configuration succeeds, status is in the file `config.status'. ### A log of configuration tests is in `config.log'. ]) AC_REVISION($Revision: 1.3 $)dnl AC_INIT([libcap-ng],[0.8.5]) AC_PREREQ([2.50])dnl AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIR([m4]) echo Configuring libcap-ng $VERSION AC_CANONICAL_TARGET AM_INIT_AUTOMAKE LT_INIT AC_SUBST(LIBTOOL_DEPS) echo . echo Checking for programs AC_PROG_CC AC_PROG_INSTALL AC_PROG_AWK echo . echo Checking for header files AC_CHECK_HEADERS(linux/capability.h, [], [AC_MSG_ERROR(linux/capability.h is required in order to build libcap-ng.)]) AC_CHECK_HEADERS(sys/xattr.h, [], [ AC_CHECK_HEADERS(attr/xattr.h, [], [AC_MSG_WARN(attr/xattr.h not found, disabling file system capabilities.)]) ]) AC_CHECK_HEADERS(linux/securebits.h, [], []) AC_CHECK_HEADERS(pthread.h, [AC_SEARCH_LIBS(pthread_atfork, pthread)], [AC_MSG_WARN(pthread.h not found, disabling pthread_atfork.)]) AC_CHECK_HEADERS(sys/vfs.h, [ AC_CHECK_HEADERS(linux/magic.h, [], [AC_MSG_WARN(linux/magic.h is required in order to verify procfs.)]) ], [AC_MSG_WARN(sys/vfs.h is required in order to verify procfs.)]) AC_ARG_WITH([capability_header], [AS_HELP_STRING([--with-capability_header=path : path to capability.h])], [CAPABILITY_HEADER=$withval], [CAPABILITY_HEADER=/usr/include/linux/capability.h]) AC_SUBST(CAPABILITY_HEADER) ALLWARNS="" ALLDEBUG="-g" OPT="-O" if test x"$GCC" = x"yes"; then OPT="-O2 -pipe" case "$target" in *linux*) ALLWARNS="-W -Wall -Wundef -Wpointer-arith -Wcast-align \ -Wwrite-strings -Waggregate-return -Wstrict-prototypes \ -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \ -Wnested-externs -Winline -Wfloat-equal -Wchar-subscripts" ;; esac fi ALLDEBUG="-g" AC_ARG_WITH(debug, [ --with-debug turn on debugging [[default=no]]], [ if test "x${withval}" = xyes; then DEBUG="$ALLDEBUG" OPT="-O" AM_CONDITIONAL(DEBUG, true) else DEBUG="-DNDEBUG" AM_CONDITIONAL(DEBUG, false) fi ], [ DEBUG="-DNDEBUG"; AM_CONDITIONAL(DEBUG, false) ]) AC_MSG_CHECKING(__attr_dealloc_free support) AC_COMPILE_IFELSE( [AC_LANG_SOURCE( [[ #include extern char *test(const char *name) __attr_dealloc_free; int main(void) { return 0; }]])], [DEALLOC="yes"], [DEALLOC="no"] ) AC_MSG_RESULT($DEALLOC) AC_ARG_WITH(warn, [ --with-warn turn on warnings [[default=yes]]], [ if test "x${withval}" = xyes; then WARNS="$ALLWARNS" else WARNS="" fi ],WARNS="$ALLWARNS") AC_SUBST(DEBUG) WFLAGS="" AC_MSG_CHECKING(for -Wformat-truncation) TMPCFLAGS="${CFLAGS}" CFLAGS="${CFLAGS} -Wformat-truncation" AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[WFLAGS="-Wno-format-truncation" AC_MSG_RESULT(yes)], [AC_MSG_RESULT(no)]) CFLAGS="${TMPCFLAGS}" AC_SUBST(WFLAGS) AC_CHECK_HEADER(sys/syscall.h, [AC_DEFINE([HAVE_SYSCALL_H], [1], [Define to 1 if you have .])], [],) dnl; This is to record in the build logs what options are being taken AC_CHECK_DECLS([PR_CAP_AMBIENT], [], [], [[#include ]]) AC_CHECK_DECLS([VFS_CAP_REVISION_2], [], [], [[#include ]]) AC_CHECK_DECLS([VFS_CAP_REVISION_3], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_EPOLLWAKEUP], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_WAKE_ALARM], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_BLOCK_SUSPEND], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_AUDIT_READ], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_PERFMON], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_BPF], [], [], [[#include ]]) AC_CHECK_DECLS([CAP_CHECKPOINT_RESTORE], [], [], [[#include ]]) dnl only build libdrop_ambient if support for ambient capabilities was found (which is normal) if test x"${ac_cv_have_decl_PR_CAP_AMBIENT}" = x"no" ; then AC_MSG_WARN("PR_CAP_AMBIENT not available - libdrop_ambient will not be built") fi AM_CONDITIONAL(BUILD_LIBDROP_AMBIENT, test x"${ac_cv_have_decl_PR_CAP_AMBIENT}" = x"yes") AC_CHECK_PROG(swig_found, swig, yes, no) if test x"${swig_found}" = x"no" ; then AC_MSG_WARN("Swig not found - python bindings will not be made") fi AM_CONDITIONAL(HAVE_SWIG, test x"${swig_found}" = x"yes") withval="" AC_MSG_CHECKING(whether to create python3 bindings) AC_ARG_WITH(python3, AS_HELP_STRING([--with-python3],[enable building python3 bindings]), use_python3=$withval, use_python3=auto) if test x$use_python3 = xno ; then AC_MSG_RESULT(no) else AC_MSG_RESULT(investigating) AC_PATH_PROG([use_python3], [python3-config], [no]) if test ${use_python3} = no ; then if test ${withval} = yes ; then echo "Python3 bindings were selected but python3-config was not found." echo "Please ensure that it's installed or pass --without-python3 to ./configure" exit 1 fi echo "Python3 bindings will NOT be built" else echo "Python3 bindings WILL be built" use_python3=yes AM_PATH_PYTHON([3.1],, [:]) PYTHON3_CFLAGS=`python3-config --cflags 2> /dev/null` PYTHON3_LIBS=`python3-config --libs 2> /dev/null` PYTHON3_INCLUDES=`python3-config --includes 2> /dev/null` AC_SUBST(PYTHON3_CFLAGS) AC_SUBST(PYTHON3_LIBS) AC_SUBST(PYTHON3_INCLUDES) fi fi AM_CONDITIONAL(USE_PYTHON3, test ${use_python3} = "yes") AC_CONFIG_FILES([Makefile src/Makefile src/libcap-ng.pc src/test/Makefile bindings/Makefile bindings/python3/Makefile bindings/src/Makefile bindings/python3/test/Makefile utils/Makefile m4/Makefile docs/Makefile]) AC_OUTPUT echo . echo " libcap-ng Version: $VERSION Target: $target Installation prefix: $prefix PYTHON_PREFIX: $PYTHON_PREFIX Compiler: $CC Compiler flags: `echo $CFLAGS | fmt -w 50 | sed 's,^, ,'` __attr_dealloc_free support: $DEALLOC " libcap-ng-0.8.5/docs/000077500000000000000000000000001460532736100143175ustar00rootroot00000000000000libcap-ng-0.8.5/docs/Makefile.am000066400000000000000000000025211460532736100163530ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,2020 Red Hat Inc. # All Rights Reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Authors: # Steve Grubb # CONFIG_CLEAN_FILES = *.rej *.orig EXTRA_DIST = $(man_MANS) man_MANS = capng_clear.3 capng_fill.3 capng_setpid.3 \ capng_get_caps_process.3 capng_update.3 capng_updatev.3 \ capng_apply.3 capng_lock.3 capng_change_id.3 capng_get_caps_fd.3 \ capng_apply_caps_fd.3 capng_have_capabilities.3 \ capng_get_rootid.3 capng_set_rootid.3 capng_have_capability.3 \ capng_print_caps_numeric.3 capng_print_caps_text.3 \ capng_name_to_capability.3 capng_capability_to_name.3 \ capng_save_state.3 capng_restore_state.3 libdrop_ambient.7 libcap-ng-0.8.5/docs/capng_apply.3000066400000000000000000000037211460532736100167030ustar00rootroot00000000000000.TH "CAPNG_APPLY" "3" "Sept 2023" "Red Hat" "Libcap-ng API" .SH NAME capng_apply \- apply the stored capabilities settings .SH "SYNOPSIS" .B #include .sp int capng_apply(capng_select_t set); .SH "DESCRIPTION" capng_apply will transfer the specified internal POSIX capabilities settings to the kernel. The options are CAPNG_SELECT_CAPS for the traditional capabilities, CAPNG_SELECT_BOUNDS for the bounding set, CAPNG_SELECT_BOTH if transferring both is desired, CAPNG_SELECT_AMBIENT if only operating on the ambient capabilities, or CAPNG_SELECT_ALL if applying all is desired. .SH "RETURN VALUE" This returns 0 on success and a negative value on failure. The values are: .RS .TP -1 not initialized .TP -2 CAPNG_SELECT_BOUNDS and failure to drop a bounding set capability .TP -3 CAPNG_SELECT_BOUNDS and failure to re-read bounding set .TP -4 CAPNG_SELECT_BOUNDS and process does not have CAP_SETPCAP .TP -5 CAPNG_SELECT_CAPS and failure in capset syscall .TP -6 CAPNG_SELECT_AMBIENT and process has no capabilities and failed clearing ambient capabilities .TP -7 CAPNG_SELECT_AMBIENT and process has capabilities and failed clearing ambient capabilities .TP -8 CAPNG_SELECT_AMBIENT and process has capabilities and failed setting an ambient capability .TP -9 Unable to acquire process capabilities to check if CAP_SETPCAP is set. .RE .SH NOTES If you are doing multi-threaded programming, calling this function will only set capabilities on the calling thread. All other threads are unaffected. If you want to set overall capabilities for a multi-threaded process, you will need to do that before creating any threads. See the capset syscall for more information on this topic. Also, bits in the bounding set can only be dropped. You cannot set them. After dropping bounding set capabilities, the bounding set is synchronized with the kernel to reflect the true state in the kernel. .SH "SEE ALSO" .BR capset (2), .BR capng_update (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_apply_caps_fd.3000066400000000000000000000014731460532736100203640ustar00rootroot00000000000000.TH "CAPNG_APPLY_CAPS_FD" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_apply_caps_fd \- write file-based capabilities to extended attributes .SH "SYNOPSIS" .B #include .sp int capng_apply_caps_fd(int fd); .SH "DESCRIPTION" This function will write the file based capabilities to the extended attributes of the file that the descriptor was opened against. The bounding set is not included in file based capabilities operations. Note that this function will only work if compiled on a kernel that supports file based capabilities such as 2.6.2 6 and later. .SH "RETURN VALUE" This returns 0 on success, -1 if something besides a regular file is passed, and -2 if a non-root namespace id is being used for rootid. .SH "SEE ALSO" .BR capng_get_caps_fd (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_capability_to_name.3000066400000000000000000000016141460532736100214000ustar00rootroot00000000000000.TH "CAPNG_CAPABILITY_TO_NAME" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_capability_to_name \- convert capability integer to text .SH "SYNOPSIS" .B #include .sp const char *capng_capability_to_name(unsigned int capability); .SH "DESCRIPTION" capng_capability_to_name will take the integer being passed and look it up to see what its text string representation would be. The integer being input must be in the valid range defined in linux/capability.h. The string that is output is the same as the define text from linux/capability.h with the CAP_ prefix removed and lower case. This is useful for taking integer representation and converting it to something more user friendly for display. .SH "RETURN VALUE" This returns a NULL pointer on failure and the correct string otherwise. .SH "SEE ALSO" .BR capng_name_to_capability (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_change_id.3000066400000000000000000000053061460532736100174600ustar00rootroot00000000000000.TH "CAPNG_CHANGE_ID" "3" "Feb 2018" "Red Hat" "Libcap-ng API" .SH NAME capng_change_id \- change the credentials retaining capabilities .SH "SYNOPSIS" .B #include .sp int capng_change_id(int uid, int gid, capng_flags_t flag); .SH "DESCRIPTION" This function will change uid and gid to the ones given while retaining the capabilities previously specified in capng_update. It is also possible to specify -1 for either the uid or gid in which case the function will not change the uid or gid and leave it "as is". This is useful if you just want the flag options to be applied (assuming the option doesn't require more privileges that you currently have). It is not necessary and perhaps better if capng_apply has not been called prior to this function so that all necessary privileges are still intact. The caller may be required to have CAP_SETPCAP capability still active before calling this function or capabilities cannot be changed. This function also takes a flag parameter that helps to tailor the exact actions performed by the function to secure the environment. The option may be or'ed together. The legal values are: .RS .TP .B CAPNG_NO_FLAG Simply change uid and retain specified capabilities and that's all. .TP .B CAPNG_DROP_SUPP_GRP After changing id, remove any supplement groups that may still be in effect from the old uid. .TP .B CAPNG_INIT_SUPP_GRP After changing id, initialize any supplement groups that may come with the new account. If given with .B CAPNG_DROP_SUPP_GRP it will have no effect. .TP .B CAPNG_CLEAR_BOUNDING Clear the bounding set regardless to the internal representation already setup prior to changing the uid/gid. .TP .B CAPNG_CLEAR_AMBIENT Clear ambient capabilities regardless of the internal representation already setup prior to changing the uid/gid. .RE .SH "RETURN VALUE" This returns 0 on success and a negative number on failure. .RS .TP -1 means capng has not been initted properly .TP -2 means a failure requesting to keep capabilities across the uid change .TP -3 means that applying the intermediate capabilities failed .TP -4 means changing gid failed .TP -5 means dropping supplemental groups failed .TP -6 means changing the uid failed .TP -7 means dropping the ability to retain caps across a uid change failed .TP -8 means clearing the bounding set failed .TP -9 means dropping CAP_SETPCAP or ambient capabilities failed .TP -10 means initializing supplemental groups failed .RE Note: the only safe action to do upon failure of this function is to probably exit. This is because you are likely in a situation with partial permissions and not what you intended. .SH "SEE ALSO" .BR capng_update (3), .BR capng_apply (3), .BR prctl (2), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_clear.3000066400000000000000000000012071460532736100166410ustar00rootroot00000000000000.TH "CAPNG_CLEAR" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_clear \- clear chosen capabilities set .SH "SYNOPSIS" .B #include .sp void capng_clear(capng_select_t set); .SH "DESCRIPTION" capng_clear sets to 0 all bits in the selected POSIX capabilities set. The options are CAPNG_SELECT_CAPS for the traditional capabilities, CAPNG_SELECT_BOUNDS for the bounding set, CAPNG_SELECT_BOTH if clearing both is desired, CAPNG_SELECT_AMBIENT if only operating on the ambient capabilities, or CAPNG_SELECT_ALL if clearing all is desired. .SH "RETURN VALUE" None. .SH "SEE ALSO" .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_fill.3000066400000000000000000000013741460532736100165060ustar00rootroot00000000000000.TH "CAPNG_FILL" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_fill \- fill chosen capabilities set .SH "SYNOPSIS" .B #include .sp void capng_fill(capng_select_t set); .SH "DESCRIPTION" capng_fill sets all bits to a 1 in the selected POSIX capabilities set. The options are CAPNG_SELECT_CAPS for the traditional capabilities, CAPNG_SELECT_BOUNDS for the bounding set, CAPNG_SELECT_BOTH if filling both is desired, CAPNG_SELECT_AMBIENT if only operating on the ambient capabilities, or CAPNG_SELECT_ALL if clearing all is desired.. .SH "RETURN VALUE" None. .SH NOTES During capng_apply, bits in the bounding set can only be dropped. You cannot set them. .SH "SEE ALSO" .BR capng_apply (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_get_caps_fd.3000066400000000000000000000014771460532736100200220ustar00rootroot00000000000000.TH "CAPNG_GET_CAPS_FD" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_get_caps_fd \- read file-based capabilities from extended attributes .SH "SYNOPSIS" .B #include .sp int capng_get_caps_fd(int fd); .SH "DESCRIPTION" This function will read the file based capabilities stored in extended attributes of the file that the descriptor was opened against. The bounding set is not included in file based capabilities operations. Note that this function will only work if compiled on a kernel that supports file based capabilities such as 2.6.26 and later. If the "magic" bit is set, then all effect capability bits are set. Otherwise the bits are cleared. .SH "RETURN VALUE" This returns 0 on success and -1 on failure. .SH "SEE ALSO" .BR capng_set_caps_fd (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_get_caps_process.3000066400000000000000000000020031460532736100210710ustar00rootroot00000000000000.TH "CAPNG_GET_CAPS_PROCESS" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_get_caps_process \- get the capabilities from a process .SH "SYNOPSIS" .B #include .sp int capng_get_caps_process(void); .SH "DESCRIPTION" capng_get_caps_process will get the capabilities and bounding set of the pid stored inside libcap-ng's state table. The default is the pid of the running process. This can be changed by using the capng_setpid function. .SH "RETURN VALUE" This returns 0 on success and -1 on failure. .SH NOTES If you are doing multi-threaded programming, calling this function will only get capabilities on the calling thread. If you want to get overall capabilities for a multi-threaded process, you can only do that before creating any threads. Afterwards, threads may be able to independently set capabilities. capng_get_caps_process needs a mounted /proc to read the current bounding set, otherwise it will fail. .SH "SEE ALSO" .BR capng_setpid (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_get_rootid.3000066400000000000000000000011501460532736100177070ustar00rootroot00000000000000.TH "CAPNG_GET_ROOTID" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_get_rootid \- get namespace root id .SH "SYNOPSIS" .B #include .sp int capng_get_rootid(void); .SH "DESCRIPTION" capng_get_rootid gets the rootid for capabilities operations. This is only applicable for file system operations. .SH "RETURN VALUE" If the file is in the init namespace or the kernel does not support V3 file system capabilities, it returns CAPNG_UNSET_ROOTID. Otherwise it return an integer for the namespace root id. .SH "SEE ALSO" .BR capng_get_caps_fd (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_have_capabilities.3000066400000000000000000000027451460532736100212170ustar00rootroot00000000000000.TH "CAPNG_HAVE_CAPABILITIES" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME .nf capng_have_capabilities, capng_have_permitted_capabilities \- check for capabilities .SH "SYNOPSIS" .B #include .sp capng_results_t capng_have_capabilities(capng_select_t set); capng_results_t capng_have_permitted_capabilities(void); .SH "DESCRIPTION" capng_have_capabilities will check the selected internal capabilities sets to see what the status is. The capabilities sets must be previously setup with calls to capng_get_caps_process, capng_get_caps_fd, or in some other way setup. The options are CAPNG_SELECT_CAPS for the traditional capabilities, CAPNG_SELECT_BOUNDS for the bounding set, CAPNG_SELECT_BOTH if checking both are desired, CAPNG_SELECT_AMBIENT if only checking the ambient capabilities, or CAPNG_SELECT_ALL if testing all sets is desired. When capabilities are checked, it will only look at the effective capabilities. If, however, the source of capabilities comes from a file, then you may need to additionally check the permitted capabilities. It's for this reason that .B capng_have_permitted_capabilities was created. It takes no arguments because it simply checks the permitted set. .SH "RETURN VALUE" This function will return one of the following four self explanatory values: CAPNG_FAIL, CAPNG_NONE, CAPNG_PARTIAL, CAPNG_FULL. .SH "SEE ALSO" .BR capng_get_caps_process (3), .BR capng_get_caps_fd (3), .BR capng_have_capability (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_have_capability.3000066400000000000000000000015511460532736100207010ustar00rootroot00000000000000.TH "CAPNG_HAVE_CAPABILITY" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_have_capability \- check for specific capability .SH "SYNOPSIS" .B #include .sp int capng_have_capability(capng_type_t which, unsigned int capability); .SH "DESCRIPTION" capng_have_capability will check the specified internal capabilities set to see if the specified capability is set. The capabilities sets must be previously setup with calls to capng_get_caps_process, capng_get_caps_fd, or in some other way setup. The values for which should be one of: CAPNG_EFFECTIVE, CAPNG_PERMITTED, CAPNG_INHERITABLE, CAPNG_BOUNDING_SET, or CAPNG_AMBIENT. .SH "RETURN VALUE" This function will return 1 if yes and 0 otherwise. .SH "SEE ALSO" .BR capng_get_caps_process (3), .BR capng_get_caps_fd (3), .BR capng_have_capabilities (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_lock.3000066400000000000000000000016711460532736100165100ustar00rootroot00000000000000.TH "CAPNG_LOCK" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_lock \- lock the current process capabilities settings .SH "SYNOPSIS" .B #include .sp int capng_lock(void); .SH "DESCRIPTION" capng_lock will take steps to prevent children of the current process to regain full privileges if the uid is 0. This should be called while possessing the CAP_SETPCAP capability in the kernel. This function will do the following if permitted by the kernel: Set the NOROOT option on for PR_SET_SECUREBITS, set the NOROOT_LOCKED option to on for PR_SET_SECUREBITS, set the PR_NO_SETUID_FIXUP option on for PR_SET_SECUREBITS, and set the PR_NO_SETUID_FIXUP_LOCKED option on for PR_SET_SECUREBITS. .SH "RETURN VALUE" This returns 0 on success and a negative number on failure. -1 means a failure setting any of the PR_SET_SECUREBITS options. .SH "SEE ALSO" .BR capng_apply (3), .BR prctl (2), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_name_to_capability.3000066400000000000000000000015731460532736100214040ustar00rootroot00000000000000.TH "CAPNG_NAME_TO_CAPABILITY" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_name_to_capability \- convert capability text to integer .SH "SYNOPSIS" .B #include .sp int capng_name_to_capability(const char *name); .SH "DESCRIPTION" capng_name_to_capability will take the string being passed and look it up to see what its integer value would be. The string being input is the same name as the define in linux/capability.h with the CAP_ prefix removed. The string case does not matter. The integer that is output is the same as the define would be from linux/capability.h. This is useful for taking string input and converting to something that can be used with capng_update. .SH "RETURN VALUE" This returns a negative number on failure and the correct define otherwise. .SH "SEE ALSO" .BR capng_capability_to_name (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_print_caps_numeric.3000066400000000000000000000023501460532736100214370ustar00rootroot00000000000000.TH "CAPNG_PRINT_CAPS_NUMERIC" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_print_caps_numeric \- print numeric values for capabilities set .SH "SYNOPSIS" .B #include .sp char *capng_print_caps_numeric(capng_print_t where, capng_select_t set); .SH "DESCRIPTION" capng_print_caps_numeric will create a numeric representation of the internal capabilities. The representation can be sent to either stdout or a buffer by passing CAPNG_PRINT_STDOUT or CAPNG_PRINT_BUFFER respectively for the where parameter. If the option was for a buffer, this function will malloc a buffer that the caller must free. The set parameter controls what is included in the representation. The legal options are CAPNG_SELECT_CAPS for the traditional capabilities, CAPNG_SELECT_BOUNDS for the bounding set, CAPNG_SELECT_BOTH if printing both is desired, CAPNG_SELECT_AMBIENT if only printing the ambient capabilities, or CAPNG_SELECT_ALL if printing all is desired. .SH "RETURN VALUE" If CAPNG_PRINT_BUFFER was selected for where, this will be the text buffer and NULL on failure. If CAPNG_PRINT_STDOUT was selected then this value will be NULL no matter what. .SH "SEE ALSO" .BR capng_print_caps_text (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_print_caps_text.3000066400000000000000000000020441460532736100207610ustar00rootroot00000000000000.TH "CAPNG_PRINT_CAPS_NUMERIC" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_print_caps_text \- print names of values for capabilities set .SH "SYNOPSIS" .B #include .sp char *capng_print_caps_text(capng_print_t where, capng_type_t which); .SH "DESCRIPTION" capng_print_caps_text will create a text string representation of the internal capability set specified. The representation can be sent to either stdout or a buffer by passing CAPNG_PRINT_STDOUT or CAPNG_PRINT_BUFFER respectively for the where parameter. If the option was for a buffer, this function will malloc a buffer that the caller must free. The legal values for the which parameter is CAPNG_EFFECTIVE, CAPNG_PERMITTED, CAPNG_INHERITABLE, CAPNG_BOUNDING_SET, or CAPNG_AMBIENT. .SH "RETURN VALUE" If CAPNG_PRINT_BUFFER was selected for where, this will be the text buffer and NULL on failure. If CAPNG_PRINT_STDOUT was selected then this value will be NULL no matter what. .SH "SEE ALSO" .BR capng_print_caps_numeric (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_restore_state.3000066400000000000000000000014371460532736100204430ustar00rootroot00000000000000.TH "CAPNG_SAVE_STATE" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_restore_state \- set the internal library state .SH "SYNOPSIS" .B #include .sp void capng_restore_state(void **state); .SH "DESCRIPTION" capng_restore_state is a function that takes the state information previously saved by capng_save_state and restores the libraries internal state. This function is not available in the python bindings. .SH "RETURN VALUE" None. .SH NOTES capng_restore_state free's the previously malloc'd state, thus the state can't be restored multiple times. The working pid is part of the restored state, if restoring the state to a different thread, capng_setpid can be used to update it. .SH "SEE ALSO" .BR capng_save_state (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_save_state.3000066400000000000000000000015421460532736100177130ustar00rootroot00000000000000.TH "CAPNG_SAVE_STATE" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_save_state \- get the internal library state .SH "SYNOPSIS" .B #include .sp void *capng_save_state(void); .SH "DESCRIPTION" capng_save_state is a function that returns a pointer to the internal state of the libcap-ng library. It should be considered opaque and not for alteration directly. This function should be used when you suspect a third party library may use libcap-ng also and want to make sure it doesn't alter something important. This function is not available in the python bindings. .SH "RETURN VALUE" This returns NULL on failure and a non-NULL pointer otherwise. .SH NOTES The structure returned by capng_save_state is malloc'd; it should be free'd if not used. .SH "SEE ALSO" .BR capng_restore_state (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_set_rootid.3000066400000000000000000000010641460532736100177270ustar00rootroot00000000000000.TH "CAPNG_SET_ROOTID" "3" "Sept 2020" "Red Hat" "Libcap-ng API" .SH NAME capng_set_rootid \- set namespace root id .SH "SYNOPSIS" .B #include .sp int capng_set_rootid(int rootid); .SH "DESCRIPTION" capng_set_rootid sets the rootid for capabilities operations. This is only applicable for file system operations. .SH "RETURN VALUE" On success, it returns 0. It returns -1 if there is an internal error or the kernel does not suppor V3 filesystem capabilities. .SH "SEE ALSO" .BR capng_set_caps_fd (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_setpid.3000066400000000000000000000017571460532736100170550ustar00rootroot00000000000000.TH "CAPNG_SETPID" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_setpid \- set working pid .SH "SYNOPSIS" .B #include .sp void capng_setpid(int pid); .SH "DESCRIPTION" capng_pid sets the working pid for capabilities operations. This is useful if you want to get the capabilities of a different process. .SH NOTES If your process calls .B fork , then the child process will still have the pid of the parent process stored in libcap-ng's internal data. It is disallowed to do any kind of setcap operations because you would be crossing process boundaries. To correct this, if your program links against pthreads, then libcap-ng will use the .B pthread_atfork function (as a weak symbol) to reset the pid information to the new process automatically. You are not required to link against pthreads. You can call .B capng_setpid and adjust the stored pid manually. .SH "RETURN VALUE" None. .SH "SEE ALSO" .BR capng_get_caps_process (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_update.3000066400000000000000000000017441460532736100170430ustar00rootroot00000000000000.TH "CAPNG_UPDATE" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_update \- update the stored capabilities settings .SH "SYNOPSIS" .B #include .sp int capng_update(capng_act_t action, capng_type_t type,unsigned int capability); .SH "DESCRIPTION" capng_update will update the internal POSIX capabilities settings based on the options passed to it. The action should be either CAPNG_DROP to set the capability bit to 0, or CAPNG_ADD to set the capability bit to 1. The operation is performed on the capability set specified in the type parameter. The values are: CAPNG_EFFECTIVE, CAPNG_PERMITTED, CAPNG_INHERITABLE, CAPNG_BOUNDING_SET, or CAPNG_AMBIENT. The values may be or'ed together to perform the same operation on multiple sets. The last parameter, capability, is the capability define as given in linux/capability.h. .SH "RETURN VALUE" This returns 0 on success and -1 on failure. .SH "SEE ALSO" .BR capng_updatev (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/capng_updatev.3000066400000000000000000000021621460532736100172240ustar00rootroot00000000000000.TH "CAPNG_UPDATEV" "3" "June 2009" "Red Hat" "Libcap-ng API" .SH NAME capng_updatev \- update the stored capabilities settings .SH "SYNOPSIS" .B #include .sp int capng_updatev(capng_act_t action, capng_type_t type, unsigned int capability, ...); .SH "DESCRIPTION" capng_updatev will update the internal POSIX capabilities settings based on the options passed to it. The action should be either CAPNG_DROP to set the capability bit to 0, or CAPNG_ADD to set the capability bit to 1. The operation is performed on the capability set specified in the type parameter. The values are: CAPNG_EFFECTIVE, CAPNG_PERMITTED, CAPNG_INHERITABLE, CAPNG_BOUNDING_SET, or CAPNG_AMBIENT. The values may be or'ed together to perform the same operation on multiple sets. The last parameter, capability, is the capability define as given in linux/capability.h. This function differs from capng_update in that you may pass a list of capabilities. This list must be terminated with a -1 value. .SH "RETURN VALUE" This returns 0 on success and -1 on failure. .SH "SEE ALSO" .BR capng_update (3), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/docs/libdrop_ambient.7000066400000000000000000000011211460532736100175340ustar00rootroot00000000000000.TH "LIBDROP_AMBIENT" "7" "Nov 2020" "Red Hat" "Libcap-ng API" .SH NAME libdrop_ambient \- force application started with ambient capabilities to drop them .SH "DESCRIPTION This library can be used via LD_PRELOAD to force an application started with ambient capabilities to drop them. It leaves other capabilities intact. This can also be linked against and automatically does the right thing. You do not need to make any calls into the library because all the work is done in the constructor which runs before main() is called. .SH "SEE ALSO" .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/libcap-ng.spec000066400000000000000000000053761460532736100161120ustar00rootroot00000000000000Summary: An alternate POSIX capabilities library Name: libcap-ng Version: 0.8.5 Release: 1%{?dist} License: LGPLv2+ Group: System Environment/Libraries URL: http://people.redhat.com/sgrubb/libcap-ng Source0: http://people.redhat.com/sgrubb/libcap-ng/%{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRequires: kernel-headers >= 2.6.11 BuildRequires: libattr-devel %description Libcap-ng is a library that makes using POSIX capabilities easier %package devel Summary: Header files for libcap-ng library License: LGPLv2+ Group: Development/Libraries Requires: kernel-headers >= 2.6.11 Requires: %{name} = %{version}-%{release} Requires: pkgconfig %description devel The libcap-ng-devel package contains the files needed for developing applications that need to use the libcap-ng library. %package python3 Summary: Python3 bindings for libcap-ng library License: LGPLv2+ Group: Development/Libraries BuildRequires: python3-devel swig Requires: %{name} = %{version}-%{release} %description python3 The libcap-ng-python3 package contains the bindings so that libcap-ng and can be used by python3 applications. %package utils Summary: Utilities for analyzing and setting file capabilities License: GPLv2+ Group: Development/Libraries %description utils The libcap-ng-utils package contains applications to analyze the POSIX capabilities of all the program running on a system. It also lets you set the file system based capabilities. %prep %setup -q %build %configure --libdir=%{_libdir} --with-python3 make CFLAGS="%{optflags}" %{?_smp_mflags} %install make DESTDIR="${RPM_BUILD_ROOT}" INSTALL='install -p' install # Remove a couple things so they don't get picked up rm -f $RPM_BUILD_ROOT/%{_libdir}/libcap-ng.la rm -f $RPM_BUILD_ROOT/%{_libdir}/libcap-ng.a rm -f $RPM_BUILD_ROOT/%{_libdir}/libdrop_ambient.la rm -f $RPM_BUILD_ROOT/%{_libdir}/libdrop_ambient.a rm -f $RPM_BUILD_ROOT/%{_libdir}/python?.?/site-packages/_capng.a rm -f $RPM_BUILD_ROOT/%{_libdir}/python?.?/site-packages/_capng.la %check make check %ldconfig_scriptlets %files %defattr(-,root,root,-) %doc COPYING.LIB /%{_libdir}/libcap-ng.so.* /%{_libdir}/libdrop_ambient.so.* %attr(0644,root,root) %{_mandir}/man7/* %files devel %defattr(-,root,root,-) %attr(0644,root,root) %{_mandir}/man3/* %attr(0644,root,root) %{_includedir}/cap-ng.h %{_libdir}/libcap-ng.so %{_libdir}/libdrop_ambient.so %attr(0644,root,root) %{_datadir}/aclocal/cap-ng.m4 %{_libdir}/pkgconfig/libcap-ng.pc %files python3 %defattr(-,root,root,-) %attr(755,root,root) %{python3_sitearch}/* %files utils %defattr(-,root,root,-) %doc COPYING %attr(0755,root,root) %{_bindir}/* %attr(0644,root,root) %{_mandir}/man8/* %changelog * Tue Apr 09 2024 Steve Grubb 0.8.5-1 - New upstream release libcap-ng-0.8.5/m4/000077500000000000000000000000001460532736100137075ustar00rootroot00000000000000libcap-ng-0.8.5/m4/Makefile.am000066400000000000000000000017161460532736100157500ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009 Red Hat Inc., Durham, North Carolina. # All Rights Reserved. # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Authors: # Steve Grubb # CONFIG_CLEAN_FILES = *.loT *.rej *.orig m4datadir = $(datadir)/aclocal dist_m4data_DATA = cap-ng.m4 libcap-ng-0.8.5/m4/cap-ng.m4000066400000000000000000000023501460532736100153160ustar00rootroot00000000000000# libcap-ng.m4 - Checks for the libcap-ng support # Copyright (c) 2009 Steve Grubb sgrubb@redhat.com # AC_DEFUN([LIBCAP_NG_PATH], [ AC_ARG_WITH(libcap-ng, [ --with-libcap-ng=[auto/yes/no] Add Libcap-ng support [default=auto]],, with_libcap_ng=auto) # Check for Libcap-ng API # # libcap-ng detection if test x$with_libcap_ng = xno ; then have_libcap_ng=no; else # Start by checking for header file AC_CHECK_HEADER(cap-ng.h, capng_headers=yes, capng_headers=no) # See if we have libcap-ng library AC_CHECK_LIB(cap-ng, capng_clear, CAPNG_LDADD=-lcap-ng,) # Check results are usable if test x$with_libcap_ng = xyes -a x$CAPNG_LDADD = x ; then AC_MSG_ERROR(libcap-ng support was requested and the library was not found) fi if test x$CAPNG_LDADD != x -a $capng_headers = no ; then AC_MSG_ERROR(libcap-ng libraries found but headers are missing) fi fi AC_SUBST(CAPNG_LDADD) AC_MSG_CHECKING(whether to use libcap-ng) if test x$CAPNG_LDADD != x ; then AC_DEFINE(HAVE_LIBCAP_NG,1,[libcap-ng support]) AC_CHECK_DECLS([CAPNG_AMBIENT], [], [], [[#include ]]) AC_MSG_RESULT(yes) else AC_MSG_RESULT(no) fi ]) libcap-ng-0.8.5/src/000077500000000000000000000000001460532736100141565ustar00rootroot00000000000000libcap-ng-0.8.5/src/Makefile.am000066400000000000000000000030261460532736100162130ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,2015,2020 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING.LIB. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # SUBDIRS = test pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libcap-ng.pc DISTCLEANFILES = $(pkgconfig_DATA) CLEANFILES = $(BUILT_SOURCES) CONFIG_CLEAN_FILES = *.loT *.rej *.orig AM_CFLAGS = -fPIC -DPIC -W -Wall -Wshadow ${WFLAGS} -Wundef -D_GNU_SOURCE AM_CPPFLAGS = -I. -I${top_srcdir} lib_LTLIBRARIES = libcap-ng.la if BUILD_LIBDROP_AMBIENT lib_LTLIBRARIES += libdrop_ambient.la endif include_HEADERS = cap-ng.h noinst_HEADERS = captab.h libcap_ng_la_SOURCES = cap-ng.c lookup_table.c libcap_ng_la_LIBADD = libcap_ng_la_DEPENDENCIES = $(libcap_ng_la_SOURCES) ../config.h libcap_ng_la_LDFLAGS = -Wl,-z,relro libdrop_ambient_la_SOURCES = libdrop_ambient.c libcap-ng-0.8.5/src/cap-ng.c000066400000000000000000001033361460532736100154750ustar00rootroot00000000000000/* libcap-ng.c -- * Copyright 2009-10, 2013, 2017, 2020-21 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include "cap-ng.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_PTHREAD_H #include // For pthread_atfork #endif #ifdef HAVE_SYSCALL_H #include #endif #ifdef HAVE_LINUX_SECUREBITS_H #include #endif #ifdef HAVE_LINUX_MAGIC_H #include #include #endif # define hidden __attribute__ ((visibility ("hidden"))) unsigned int last_cap hidden = 0; /* * Some milestones of when things became available: * 2.6.24 kernel XATTR_NAME_CAPS * 2.6.25 kernel PR_CAPBSET_DROP, CAPABILITY_VERSION_2 * 2.6.26 kernel PR_SET_SECUREBITS, SECURE_*_LOCKED, VERSION_3 * 3.5 kernel PR_SET_NO_NEW_PRIVS * 4.3 kernel PR_CAP_AMBIENT * 4.14 kernel VFS_CAP_REVISION_3 */ #ifdef PR_CAPBSET_DROP static int HAVE_PR_CAPBSET_DROP = 0; #endif #ifdef PR_SET_SECUREBITS static int HAVE_PR_SET_SECUREBITS = 0; #endif #ifdef PR_SET_NO_NEW_PRIVS static int HAVE_PR_SET_NO_NEW_PRIVS = 0; #endif #ifdef PR_CAP_AMBIENT static int HAVE_PR_CAP_AMBIENT = 0; #endif /* External syscall prototypes */ extern int capset(cap_user_header_t header, cap_user_data_t data); extern int capget(cap_user_header_t header, const cap_user_data_t data); // Local functions static void update_bounding_set(capng_act_t action, unsigned int capability, unsigned int idx); static void update_ambient_set(capng_act_t action, unsigned int capability, unsigned int idx); // Local defines #define MASK(x) (1U << (x)) #ifdef PR_CAPBSET_DROP #define UPPER_MASK ~((~0U)<<(last_cap-31)) #else // For v1 systems UPPER_MASK will never be used #define UPPER_MASK (unsigned)(~0U) #endif // Re-define cap_valid so its uniform between V1 and V3 #undef cap_valid #define cap_valid(x) ((x) <= last_cap) // If we don't have the xattr library, then we can't // compile-in file system capabilities #if !defined(HAVE_ATTR_XATTR_H) && !defined (HAVE_SYS_XATTR_H) #undef VFS_CAP_U32 #endif #ifdef VFS_CAP_U32 #ifdef HAVE_SYS_XATTR_H #include #else #ifdef HAVE_ATTR_XATTR_H #include #endif #endif #if __BYTE_ORDER == __BIG_ENDIAN #define FIXUP(x) bswap_32(x) #else #define FIXUP(x) (x) #endif #endif #ifndef _LINUX_CAPABILITY_VERSION_1 #define _LINUX_CAPABILITY_VERSION_1 0x19980330 #endif #ifndef _LINUX_CAPABILITY_VERSION_2 #define _LINUX_CAPABILITY_VERSION_2 0x20071026 #endif #ifndef _LINUX_CAPABILITY_VERSION_3 #define _LINUX_CAPABILITY_VERSION_3 0x20080522 #endif // This public API went private in the 2.6.36 kernel - hope it never changes #ifndef XATTR_CAPS_SUFFIX #define XATTR_CAPS_SUFFIX "capability" #endif #ifndef XATTR_SECURITY_PREFIX #define XATTR_SECURITY_PREFIX "security." #endif #ifndef XATTR_NAME_CAPS #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX #endif /* Child processes can't get caps back */ #ifndef SECURE_NOROOT #define SECURE_NOROOT 0 #endif #ifndef SECURE_NOROOT_LOCKED #define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */ #endif /* Setuid apps run by uid 0 don't get caps back */ #ifndef SECURE_NO_SETUID_FIXUP #define SECURE_NO_SETUID_FIXUP 2 #endif #ifndef SECURE_NO_SETUID_FIXUP_LOCKED #define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */ #endif #ifndef VFS_CAP_U32 #define VFS_CAP_U32 2 #endif #if (VFS_CAP_U32 != 2) #error VFS_CAP_U32 does not match the library, you need a new version #endif // States: new, allocated, initted, updated, applied typedef enum { CAPNG_NEW, CAPNG_ERROR, CAPNG_ALLOCATED, CAPNG_INIT, CAPNG_UPDATED, CAPNG_APPLIED } capng_states_t; // Create an easy data struct out of the kernel definitions typedef union { struct __user_cap_data_struct v1; struct __user_cap_data_struct v3[VFS_CAP_U32]; } cap_data_t; // This struct keeps all state info struct cap_ng { int cap_ver; int vfs_cap_ver; struct __user_cap_header_struct hdr; cap_data_t data; capng_states_t state; __le32 rootid; __u32 bounds[VFS_CAP_U32]; __u32 ambient[VFS_CAP_U32]; }; // Global variables with per thread uniqueness static __thread struct cap_ng m = { 1, 1, {0, 0}, { {0, 0, 0} }, CAPNG_NEW, CAPNG_UNSET_ROOTID, {0, 0}, {0, 0} }; /* * Reset the state so that init gets called to erase everything */ static void deinit(void) { m.state = CAPNG_NEW; } static inline int test_cap(unsigned int cap) { // prctl returns 0 or 1 for valid caps, -1 otherwise return prctl(PR_CAPBSET_READ, cap) >= 0; } // The maximum cap value is determined by VFS_CAP_U32 #define MAX_CAP_VALUE (VFS_CAP_U32 * sizeof(__le32) * 8) static void init_lib(void) __attribute__ ((constructor)); static void init_lib(void) { // This is so dynamic libraries don't re-init static unsigned int run_once = 0; if (run_once) return; run_once = 1; #ifdef HAVE_PTHREAD_H pthread_atfork(NULL, NULL, deinit); #endif // Detect last cap if (last_cap == 0) { int fd; // Try to read last cap from procfs fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY); if (fd >= 0) { #ifdef HAVE_LINUX_MAGIC_H struct statfs st; // Bail out if procfs is invalid or fstatfs fails if (fstatfs(fd, &st) || st.f_type != PROC_SUPER_MAGIC) goto fail; #endif char buf[8]; int num = read(fd, buf, sizeof(buf) - 1); if (num > 0) { buf[num] = 0; errno = 0; unsigned int val = strtoul(buf, NULL, 10); if (errno == 0) last_cap = val; } fail: close(fd); } // Run a binary search over capabilities if (last_cap == 0) { // starting with last_cap=MAX_CAP_VALUE means we always know // that cap1 is invalid after the first iteration last_cap = MAX_CAP_VALUE; unsigned int cap0 = 0, cap1 = MAX_CAP_VALUE; while (cap0 < last_cap) { if (test_cap(last_cap)) cap0 = last_cap; else cap1 = last_cap; last_cap = (cap0 + cap1) / 2U; } } } // Detect prctl options at runtime #ifdef PR_CAPBSET_DROP errno = 0; prctl(PR_CAPBSET_READ, 0, 0, 0, 0); if (!errno) HAVE_PR_CAPBSET_DROP = 1; #endif #ifdef PR_SET_SECUREBITS errno = 0; prctl(PR_GET_SECUREBITS, 0, 0, 0, 0); if (!errno) HAVE_PR_SET_SECUREBITS = 1; #endif #ifdef PR_SET_NO_NEW_PRIVS errno = 0; prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0); if (!errno) HAVE_PR_SET_NO_NEW_PRIVS = 1; #endif #ifdef PR_CAP_AMBIENT errno = 0; prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, 0, 0, 0); if (!errno) HAVE_PR_CAP_AMBIENT = 1; #endif } static void init(void) { // This is so static libs get initialized init_lib(); if (m.state != CAPNG_NEW) return; memset(&m.hdr, 0, sizeof(m.hdr)); (void)capget(&m.hdr, NULL); // Returns -EINVAL if (m.hdr.version == _LINUX_CAPABILITY_VERSION_3 || m.hdr.version == _LINUX_CAPABILITY_VERSION_2) { m.cap_ver = 3; } else if (m.hdr.version == _LINUX_CAPABILITY_VERSION_1) { m.cap_ver = 1; } else { m.state = CAPNG_ERROR; return; } #if VFS_CAP_REVISION == VFS_CAP_REVISION_1 m.vfs_cap_ver = 1; #else m.vfs_cap_ver = 2; // Intentionally set to 2 for both 2 & 3 #endif memset(&m.data, 0, sizeof(cap_data_t)); #ifdef HAVE_SYSCALL_H m.hdr.pid = (unsigned)syscall(__NR_gettid); #else m.hdr.pid = (unsigned)getpid(); #endif m.rootid = CAPNG_UNSET_ROOTID; m.state = CAPNG_ALLOCATED; } void capng_clear(capng_select_t set) { if (m.state == CAPNG_NEW) init(); if (m.state == CAPNG_ERROR) return; if (set & CAPNG_SELECT_CAPS) memset(&m.data, 0, sizeof(cap_data_t)); #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { if (set & CAPNG_SELECT_BOUNDS) memset(m.bounds, 0, sizeof(m.bounds)); } #endif #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { if (set & CAPNG_SELECT_AMBIENT) memset(m.ambient, 0, sizeof(m.ambient)); } #endif m.state = CAPNG_INIT; } void capng_fill(capng_select_t set) { if (m.state == CAPNG_NEW) init(); if (m.state == CAPNG_ERROR) return; if (set & CAPNG_SELECT_CAPS) { if (m.cap_ver == 1) { m.data.v1.effective = 0x7FFFFFFFU; m.data.v1.permitted = 0x7FFFFFFFU; m.data.v1.inheritable = 0; } else { m.data.v3[0].effective = 0xFFFFFFFFU; m.data.v3[0].permitted = 0xFFFFFFFFU; m.data.v3[0].inheritable = 0; m.data.v3[1].effective = 0xFFFFFFFFU; m.data.v3[1].permitted = 0xFFFFFFFFU; m.data.v3[1].inheritable = 0; } } #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { if (set & CAPNG_SELECT_BOUNDS) { unsigned i; for (i=0; i>5); i++; } while (cap_valid(i)); return 0; } #endif #ifdef PR_CAP_AMBIENT static int get_ambient_set(void) { char buf[64]; FILE *f; int rc; snprintf(buf, sizeof(buf), "/proc/%d/status", m.hdr.pid ? m.hdr.pid : #ifdef HAVE_SYSCALL_H (int)syscall(__NR_gettid)); #else (int)getpid()); #endif f = fopen(buf, "re"); if (f) { __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (strncmp(buf, "CapA", 4)) continue; sscanf(buf, "CapAmb: %08x%08x", &m.ambient[1], &m.ambient[0]); fclose(f); return 0; } fclose(f); // Didn't find ambient set, fall through and try prctl way } // Might be in a container with no procfs - do it the hard way memset(m.ambient, 0, sizeof(m.ambient)); unsigned int i = 0; do { rc = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0); if (rc < 0) return -1; // Just add set bits if (rc) update_ambient_set(CAPNG_ADD, i%32, i>>5); i++; } while (cap_valid(i)); return 0; } #endif /* * Returns 0 on success and -1 on failure */ int capng_get_caps_process(void) { int rc; if (m.state == CAPNG_NEW) init(); if (m.state == CAPNG_ERROR) return -1; rc = capget((cap_user_header_t)&m.hdr, (cap_user_data_t)&m.data); if (rc == 0) { m.state = CAPNG_INIT; #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { rc = get_bounding_set(); if (rc < 0) m.state = CAPNG_ERROR; } #endif #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { rc = get_ambient_set(); if (rc < 0) m.state = CAPNG_ERROR; } #endif } return rc; } #ifdef VFS_CAP_U32 #ifdef VFS_CAP_REVISION_3 static int load_data(const struct vfs_ns_cap_data *filedata, int size) #else static int load_data(const struct vfs_cap_data *filedata, int size) #endif { unsigned int magic; if (m.cap_ver == 1) return -1; // Should never get here but just in case magic = FIXUP(filedata->magic_etc); switch (magic & VFS_CAP_REVISION_MASK) { case VFS_CAP_REVISION_1: m.vfs_cap_ver = 1; if (size != XATTR_CAPS_SZ_1) return -1; break; case VFS_CAP_REVISION_2: m.vfs_cap_ver = 2; if (size != XATTR_CAPS_SZ_2) return -1; break; #ifdef VFS_CAP_REVISION_3 case VFS_CAP_REVISION_3: m.vfs_cap_ver = 3; if (size != XATTR_CAPS_SZ_3) return -1; break; #endif default: return -1; } // Now stuff the data structures m.data.v3[0].permitted = FIXUP(filedata->data[0].permitted); m.data.v3[1].permitted = FIXUP(filedata->data[1].permitted); m.data.v3[0].inheritable = FIXUP(filedata->data[0].inheritable); m.data.v3[1].inheritable = FIXUP(filedata->data[1].inheritable); if (magic & VFS_CAP_FLAGS_EFFECTIVE) { m.data.v3[0].effective = m.data.v3[0].permitted | m.data.v3[0].inheritable; m.data.v3[1].effective = m.data.v3[1].permitted | m.data.v3[1].inheritable; } else { m.data.v3[0].effective = 0; m.data.v3[1].effective = 0; } #ifdef VFS_CAP_REVISION_3 if (size == XATTR_CAPS_SZ_3) { struct vfs_ns_cap_data *d = (struct vfs_ns_cap_data *)filedata; m.rootid = FIXUP(d->rootid); } #endif return 0; } #endif int capng_get_caps_fd(int fd) { #ifndef VFS_CAP_U32 return -1; #else int rc; #ifdef VFS_CAP_REVISION_3 struct vfs_ns_cap_data filedata; #else struct vfs_cap_data filedata; #endif if (m.state == CAPNG_NEW) init(); if (m.state == CAPNG_ERROR) return -1; rc = fgetxattr(fd, XATTR_NAME_CAPS, &filedata, sizeof(filedata)); if (rc <= 0) return -1; rc = load_data(&filedata, rc); if (rc == 0) m.state = CAPNG_INIT; else m.state = CAPNG_ERROR; // If load data failed, malformed data return rc; #endif } static void v1_update(capng_act_t action, unsigned int capability, __u32 *data) { if (action == CAPNG_ADD) *data |= MASK(capability); else *data &= ~(MASK(capability)); } static void update_effective(capng_act_t action, unsigned int capability, unsigned int idx) { if (action == CAPNG_ADD) m.data.v3[idx].effective |= MASK(capability); else m.data.v3[idx].effective &= ~(MASK(capability)); } static void update_permitted(capng_act_t action, unsigned int capability, unsigned int idx) { if (action == CAPNG_ADD) m.data.v3[idx].permitted |= MASK(capability); else m.data.v3[idx].permitted &= ~(MASK(capability)); } static void update_inheritable(capng_act_t action, unsigned int capability, unsigned int idx) { if (action == CAPNG_ADD) m.data.v3[idx].inheritable |= MASK(capability); else m.data.v3[idx].inheritable &= ~(MASK(capability)); } static void update_bounding_set(capng_act_t action, unsigned int capability, unsigned int idx) { #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { if (action == CAPNG_ADD) m.bounds[idx] |= MASK(capability); else m.bounds[idx] &= ~(MASK(capability)); } #endif } static void update_ambient_set(capng_act_t action, unsigned int capability, unsigned int idx) { #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { if (action == CAPNG_ADD) m.ambient[idx] |= MASK(capability); else m.ambient[idx] &= ~(MASK(capability)); } #endif } int capng_update(capng_act_t action, capng_type_t type, unsigned int capability) { // Before updating, we expect that the data is initialized to something if (m.state < CAPNG_INIT) return -1; if (!cap_valid(capability)) { errno = EINVAL; return -1; } if (m.cap_ver == 1) { if (CAPNG_EFFECTIVE & type) v1_update(action, capability, &m.data.v1.effective); if (CAPNG_PERMITTED & type) v1_update(action, capability, &m.data.v1.permitted); if (CAPNG_INHERITABLE & type) v1_update(action, capability, &m.data.v1.inheritable); } else { unsigned int idx; if (capability > 31) { idx = capability>>5; capability %= 32; } else idx = 0; if (CAPNG_EFFECTIVE & type) update_effective(action, capability, idx); if (CAPNG_PERMITTED & type) update_permitted(action, capability, idx); if (CAPNG_INHERITABLE & type) update_inheritable(action, capability, idx); if (CAPNG_BOUNDING_SET & type) update_bounding_set(action, capability, idx); if (CAPNG_AMBIENT & type) update_ambient_set(action, capability, idx); } m.state = CAPNG_UPDATED; return 0; } int capng_updatev(capng_act_t action, capng_type_t type, unsigned int capability, ...) { int rc; unsigned int cap; va_list ap; rc = capng_update(action, type, capability); if (rc) return rc; va_start(ap, capability); cap = va_arg(ap, unsigned int); while (cap_valid(cap)) { rc = capng_update(action, type, cap); if (rc) break; cap = va_arg(ap, unsigned int); } va_end(ap); // See if planned exit or invalid if (cap == (unsigned)-1) rc = 0; else { rc = -1; errno = EINVAL; } return rc; } int capng_apply(capng_select_t set) { int rc = 0; // Before updating, we expect that the data is initialized to something if (m.state < CAPNG_INIT) return -1; if (set & CAPNG_SELECT_BOUNDS) { #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { struct cap_ng state; memcpy(&state, &m, sizeof(state)); /* save state */ if (capng_get_caps_process()) return -9; if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP)) { unsigned int i; memcpy(&m, &state, sizeof(m)); /* restore state */ for (i=0; i <= last_cap; i++) { if (capng_have_capability(CAPNG_BOUNDING_SET, i) == 0) { if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) <0) { rc = -2; goto try_caps; } } } m.state = CAPNG_APPLIED; if (get_bounding_set() < 0) { rc = -3; goto try_caps; } } else { memcpy(&m, &state, sizeof(m)); /* restore state */ rc = -4; goto try_caps; } } #endif } // Try caps is here so that if someone had SELECT_BOTH and we blew up // doing the bounding set, we at least try to set any capabilities // before returning in case the caller also doesn't bother checking // the return code. try_caps: if (set & CAPNG_SELECT_CAPS) { if (capset((cap_user_header_t)&m.hdr, (cap_user_data_t)&m.data) == 0) m.state = CAPNG_APPLIED; else rc = -5; } // Most programs do not and should not mess with ambient capabilities. // Instead of returning here if rc is set, we'll let it try to // do something with ambient capabilities in hopes that it's lowering // capabilities. Again, this is for people that don't check their // return codes. // // Do ambient last so that inheritable and permitted are set by the // time we get here. if (set & CAPNG_SELECT_AMBIENT) { #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { if (capng_have_capabilities(CAPNG_SELECT_AMBIENT) == CAPNG_NONE) { if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) { rc = -6; goto out; } } else { unsigned int i; // Clear them all if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) { rc = -7; goto out; } for (i=0; i <= last_cap; i++) { if (capng_have_capability(CAPNG_AMBIENT, i)) if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0){ rc = -8; goto out; } } } m.state = CAPNG_APPLIED; } #endif } out: return rc; } #ifdef VFS_CAP_U32 #ifdef VFS_CAP_REVISION_3 static int save_data(struct vfs_ns_cap_data *filedata, int *size) #else static int save_data(struct vfs_cap_data *filedata, int *size) #endif { // Now stuff the data structures if (m.vfs_cap_ver == 1) { filedata->data[0].permitted = FIXUP(m.data.v1.permitted); filedata->data[0].inheritable = FIXUP(m.data.v1.inheritable); filedata->magic_etc = FIXUP(VFS_CAP_REVISION_1); *size = XATTR_CAPS_SZ_1; } else if (m.vfs_cap_ver == 2 || m.vfs_cap_ver == 3) { int eff; if (m.data.v3[0].effective || m.data.v3[1].effective) eff = VFS_CAP_FLAGS_EFFECTIVE; else eff = 0; filedata->data[0].permitted = FIXUP(m.data.v3[0].permitted); filedata->data[0].inheritable = FIXUP(m.data.v3[0].inheritable); filedata->data[1].permitted = FIXUP(m.data.v3[1].permitted); filedata->data[1].inheritable = FIXUP(m.data.v3[1].inheritable); filedata->magic_etc = FIXUP(VFS_CAP_REVISION_2 | eff); *size = XATTR_CAPS_SZ_2; } #ifdef VFS_CAP_REVISION_3 if (m.vfs_cap_ver == 3) { // Kernel doesn't support namespaces with non-0 rootid if (m.rootid!= 0) return -1; filedata->rootid = FIXUP(m.rootid); *size = XATTR_CAPS_SZ_3; } #endif return 0; } #endif int capng_apply_caps_fd(int fd) { #ifndef VFS_CAP_U32 return -1; #else int rc, size = 0; #ifdef VFS_CAP_REVISION_3 struct vfs_ns_cap_data filedata; #else struct vfs_cap_data filedata; #endif struct stat buf; // Before updating, we expect that the data is initialized to something if (m.state < CAPNG_INIT) return -1; if (fstat(fd, &buf) != 0) return -1; if (S_ISLNK(buf.st_mode) || !S_ISREG(buf.st_mode)) { errno = EINVAL; return -1; } if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) rc = fremovexattr(fd, XATTR_NAME_CAPS); else { if (save_data(&filedata, &size)) { m.state = CAPNG_ERROR; errno = EINVAL; return -2; } rc = fsetxattr(fd, XATTR_NAME_CAPS, &filedata, size, 0); } if (rc == 0) m.state = CAPNG_APPLIED; return rc; #endif } // Change uids keeping/removing only certain capabilities // flag to drop supp groups int capng_change_id(int uid, int gid, capng_flags_t flag) { int rc, ret, need_setgid, need_setuid; // Before updating, we expect that the data is initialized to something if (m.state < CAPNG_INIT) return -1; // Check the current capabilities #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { // If newer kernel, we need setpcap to change the bounding set if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP) == 0 && flag & CAPNG_CLEAR_BOUNDING) capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETPCAP); } #endif if (gid == -1 || capng_have_capability(CAPNG_EFFECTIVE, CAP_SETGID)) need_setgid = 0; else { need_setgid = 1; capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETGID); } if (uid == -1 || capng_have_capability(CAPNG_EFFECTIVE, CAP_SETUID)) need_setuid = 0; else { need_setuid = 1; capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETUID); } // Tell system we want to keep caps across uid change if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0)) return -2; // Change to the temp capabilities rc = capng_apply(CAPNG_SELECT_CAPS); if (rc < 0) { ret = -3; goto err_out; } // If we are clearing ambient, only clear since its applied at the end if (flag & CAPNG_CLEAR_AMBIENT) capng_clear(CAPNG_SELECT_AMBIENT); // Clear bounding set if needed while we have CAP_SETPCAP if (flag & CAPNG_CLEAR_BOUNDING) { capng_clear(CAPNG_SELECT_BOUNDS); rc = capng_apply(CAPNG_SELECT_BOUNDS); if (rc) { ret = -8; goto err_out; } } // Change gid if (gid != -1) { rc = setresgid(gid, gid, gid); if (rc) { ret = -4; goto err_out; } } // See if we need to init supplemental groups if ((flag & CAPNG_INIT_SUPP_GRP) && uid != -1) { struct passwd *pw = getpwuid(uid); if (pw == NULL) { ret = -10; goto err_out; } if (gid != -1) { if (initgroups(pw->pw_name, gid)) { ret = -5; goto err_out; } } else if (initgroups(pw->pw_name, pw->pw_gid)) { ret = -5; goto err_out; } } // See if we need to unload supplemental groups if ((flag & CAPNG_DROP_SUPP_GRP) && gid != -1) { if (setgroups(0, NULL)) { ret = -5; goto err_out; } } // Change uid if (uid != -1) { rc = setresuid(uid, uid, uid); if (rc) { ret = -6; goto err_out; } } // Tell it we are done keeping capabilities rc = prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0); if (rc) return -7; // Now throw away CAP_SETPCAP so no more changes if (need_setgid) capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETGID); if (need_setuid) capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETUID); // Now drop setpcap & apply capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETPCAP); rc = capng_apply(CAPNG_SELECT_CAPS|CAPNG_SELECT_AMBIENT); if (rc < 0) return -9; // Done m.state = CAPNG_UPDATED; return 0; err_out: prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0); return ret; } int capng_lock(void) { // If either fail, return -1 since something is not right #ifdef PR_SET_SECUREBITS if (HAVE_PR_SET_SECUREBITS) { int rc = prctl(PR_SET_SECUREBITS, 1 << SECURE_NOROOT | 1 << SECURE_NOROOT_LOCKED | 1 << SECURE_NO_SETUID_FIXUP | 1 << SECURE_NO_SETUID_FIXUP_LOCKED, 0, 0, 0); #ifdef PR_SET_NO_NEW_PRIVS if (HAVE_PR_SET_NO_NEW_PRIVS) { if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) return -1; } #endif if (rc) return -1; } #endif return 0; } // -1 - error, 0 - no caps, 1 partial caps, 2 full caps capng_results_t capng_have_capabilities(capng_select_t set) { int empty = 0, full = 0; // First, try to init with current set if (m.state < CAPNG_INIT) { if (capng_get_caps_process()) return CAPNG_FAIL; } // If we still don't have anything, error out if (m.state < CAPNG_INIT) return CAPNG_FAIL; if (set & CAPNG_SELECT_CAPS) { if (m.cap_ver == 1) { if (m.data.v1.effective == 0) empty = 1; // after fill, 30 bits starts from upper to lower else if (m.data.v1.effective == 0x7FFFFFFFU) full = 1; // actual capabilities read from system else if (m.data.v1.effective == 0xFFFFFEFFU) full = 1; else return CAPNG_PARTIAL; } else { if (m.data.v3[0].effective == 0) empty = 1; else if (m.data.v3[0].effective == 0xFFFFFFFFU) full = 1; else return CAPNG_PARTIAL; if ((m.data.v3[1].effective & UPPER_MASK) == 0 && !full) empty = 1; else if ((m.data.v3[1].effective & UPPER_MASK) == UPPER_MASK && !empty) full = 1; else return CAPNG_PARTIAL; } } #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { if (set & CAPNG_SELECT_BOUNDS) { if (m.bounds[0] == 0) empty = 1; else if (m.bounds[0] == 0xFFFFFFFFU) full = 1; else return CAPNG_PARTIAL; if ((m.bounds[1] & UPPER_MASK) == 0) empty = 1; else if ((m.bounds[1] & UPPER_MASK) == UPPER_MASK) full = 1; else return CAPNG_PARTIAL; } } else empty = 1; #endif #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { if (set & CAPNG_SELECT_AMBIENT) { if (m.ambient[0] == 0) empty = 1; else if (m.ambient[0] == 0xFFFFFFFFU) full = 1; else return CAPNG_PARTIAL; if ((m.ambient[1] & UPPER_MASK) == 0) empty = 1; else if ((m.ambient[1] & UPPER_MASK) == UPPER_MASK) full = 1; else return CAPNG_PARTIAL; } } else empty = 1; #endif if (empty == 1 && full == 0) return CAPNG_NONE; else if (empty == 0 && full == 1) return CAPNG_FULL; return CAPNG_PARTIAL; } // -1 - error, 0 - no caps, 1 partial caps, 2 full caps capng_results_t capng_have_permitted_capabilities(void) { int empty = 0, full = 0; // First, try to init with current set if (m.state < CAPNG_INIT) { if (capng_get_caps_process()) return CAPNG_FAIL; } // If we still don't have anything, error out if (m.state < CAPNG_INIT) return CAPNG_FAIL; if (m.data.v3[0].permitted == 0) empty = 1; else if (m.data.v3[0].permitted == 0xFFFFFFFFU) full = 1; else return CAPNG_PARTIAL; if ((m.data.v3[1].permitted & UPPER_MASK) == 0 && !full) empty = 1; else if ((m.data.v3[1].permitted & UPPER_MASK) == UPPER_MASK && !empty) full = 1; else return CAPNG_PARTIAL; if (empty == 1 && full == 0) return CAPNG_NONE; else if (empty == 0 && full == 1) return CAPNG_FULL; return CAPNG_PARTIAL; } static int check_effective(unsigned int capability, unsigned int idx) { return MASK(capability) & m.data.v3[idx].effective ? 1 : 0; } static int check_permitted(unsigned int capability, unsigned int idx) { return MASK(capability) & m.data.v3[idx].permitted ? 1 : 0; } static int check_inheritable(unsigned int capability, unsigned int idx) { return MASK(capability) & m.data.v3[idx].inheritable ? 1 : 0; } static int bounds_bit_check(unsigned int capability, unsigned int idx) { #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { return MASK(capability) & m.bounds[idx] ? 1 : 0; } #endif return 0; } static int ambient_bit_check(unsigned int capability, unsigned int idx) { #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { return MASK(capability) & m.ambient[idx] ? 1 : 0; } #endif return 0; } static int v1_check(unsigned int capability, __u32 data) { return MASK(capability) & data ? 1 : 0; } int capng_have_capability(capng_type_t which, unsigned int capability) { // First, try to init with current set if (m.state < CAPNG_INIT) { if (capng_get_caps_process()) return 0; } // If we still don't have anything, error out if (m.state < CAPNG_INIT) return 0; if (m.cap_ver == 1 && capability > 31) return 0; if (!cap_valid(capability)) return 0; if (m.cap_ver == 1) { if (which == CAPNG_EFFECTIVE) return v1_check(capability, m.data.v1.effective); else if (which == CAPNG_PERMITTED) return v1_check(capability, m.data.v1.permitted); else if (which == CAPNG_INHERITABLE) return v1_check(capability, m.data.v1.inheritable); } else { unsigned int idx; if (capability > 31) { idx = capability>>5; capability %= 32; } else idx = 0; if (which == CAPNG_EFFECTIVE) return check_effective(capability, idx); else if (which == CAPNG_PERMITTED) return check_permitted(capability, idx); else if (which == CAPNG_INHERITABLE) return check_inheritable(capability, idx); else if (which == CAPNG_BOUNDING_SET) return bounds_bit_check(capability, idx); else if (which == CAPNG_AMBIENT) return ambient_bit_check(capability, idx); } return 0; } char *capng_print_caps_numeric(capng_print_t where, capng_select_t set) { char *ptr = NULL; if (m.state < CAPNG_INIT) return ptr; if (where == CAPNG_PRINT_STDOUT) { if (set & CAPNG_SELECT_CAPS) { if (m.cap_ver == 1) { printf( "Effective: %08X\n" "Permitted: %08X\n" "Inheritable: %08X\n", m.data.v1.effective, m.data.v1.permitted, m.data.v1.inheritable); } else { printf( "Effective: %08X, %08X\n" "Permitted: %08X, %08X\n" "Inheritable: %08X, %08X\n", m.data.v3[1].effective & UPPER_MASK, m.data.v3[0].effective, m.data.v3[1].permitted & UPPER_MASK, m.data.v3[0].permitted, m.data.v3[1].inheritable & UPPER_MASK, m.data.v3[0].inheritable); } } #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { if (set & CAPNG_SELECT_BOUNDS) printf("Bounding Set: %08X, %08X\n", m.bounds[1] & UPPER_MASK, m.bounds[0]); } #endif #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { if (set & CAPNG_SELECT_AMBIENT) printf("Ambient : %08X, %08X\n", m.ambient[1] & UPPER_MASK, m.ambient[0]); } #endif } else if (where == CAPNG_PRINT_BUFFER) { if (set & CAPNG_SELECT_CAPS) { // Make it big enough for bounding & ambient set, too ptr = malloc(160); if (m.cap_ver == 1) { snprintf(ptr, 160, "Effective: %08X\n" "Permitted: %08X\n" "Inheritable: %08X\n", m.data.v1.effective, m.data.v1.permitted, m.data.v1.inheritable); } else { snprintf(ptr, 160, "Effective: %08X, %08X\n" "Permitted: %08X, %08X\n" "Inheritable: %08X, %08X\n", m.data.v3[1].effective & UPPER_MASK, m.data.v3[0].effective, m.data.v3[1].permitted & UPPER_MASK, m.data.v3[0].permitted, m.data.v3[1].inheritable & UPPER_MASK, m.data.v3[0].inheritable); } } if (set & CAPNG_SELECT_BOUNDS) { #ifdef PR_CAPBSET_DROP if (HAVE_PR_CAPBSET_DROP) { char *s; // If ptr is NULL, we only room for bounding and ambient if (ptr == NULL ) { ptr = malloc(80); if (ptr == NULL) return ptr; *ptr = 0; s = ptr; } else s = ptr + strlen(ptr); snprintf(s, 40, "Bounding Set: %08X, %08X\n", m.bounds[1] & UPPER_MASK, m.bounds[0]); } #endif } if (set & CAPNG_SELECT_AMBIENT) { #ifdef PR_CAP_AMBIENT if (HAVE_PR_CAP_AMBIENT) { char *s; // If ptr is NULL, we only room for ambient if (ptr == NULL ) { ptr = malloc(40); if (ptr == NULL) return ptr; *ptr = 0; s = ptr; } else s = ptr + strlen(ptr); snprintf(s, 40, "Ambient Set: %08X, %08X\n", m.ambient[1] & UPPER_MASK, m.ambient[0]); } #endif } } return ptr; } char *capng_print_caps_text(capng_print_t where, capng_type_t which) { unsigned int i, once = 0, cnt = 0; char *ptr = NULL; if (m.state < CAPNG_INIT) return ptr; for (i=0; i<=last_cap; i++) { if (capng_have_capability(which, i)) { const char *n = capng_capability_to_name(i); if (n == NULL) n = "unknown"; if (where == CAPNG_PRINT_STDOUT) { if (once == 0) { printf("%s", n); once++; } else printf(", %s", n); } else if (where == CAPNG_PRINT_BUFFER) { int len; if (once == 0) { ptr = malloc(last_cap*20); if (ptr == NULL) return ptr; len = sprintf(ptr+cnt, "%s", n); once++; } else len = sprintf(ptr+cnt, ", %s", n); if (len > 0) cnt+=len; } } } if (once == 0) { if (where == CAPNG_PRINT_STDOUT) printf("none"); else ptr = strdup("none"); } return ptr; } void *capng_save_state(void) { void *ptr = malloc(sizeof(m)); if (ptr) memcpy(ptr, &m, sizeof(m)); return ptr; } void capng_restore_state(void **state) { if (state) { void *ptr = *state; if (ptr) memcpy(&m, ptr, sizeof(m)); free(ptr); *state = NULL; } } libcap-ng-0.8.5/src/cap-ng.h000066400000000000000000000071751460532736100155060ustar00rootroot00000000000000/* libcap-ng.h -- * Copyright 2009,2013,2020-23 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #ifndef LIBCAP_NG_HEADER #define LIBCAP_NG_HEADER #include #include #include // The next 2 macros originate in sys/cdefs.h // gcc-analyzer notation #ifndef __attr_dealloc_free # define __attr_dealloc_free #endif // Warn unused result #ifndef __wur # define __wur #endif #ifdef __cplusplus extern "C" { #endif typedef enum { CAPNG_DROP, CAPNG_ADD } capng_act_t; typedef enum { CAPNG_EFFECTIVE=1, CAPNG_PERMITTED=2, CAPNG_INHERITABLE=4, CAPNG_BOUNDING_SET=8, CAPNG_AMBIENT=16 } capng_type_t; typedef enum { CAPNG_SELECT_CAPS = 16, CAPNG_SELECT_BOUNDS = 32, CAPNG_SELECT_BOTH = 48, CAPNG_SELECT_AMBIENT = 64, CAPNG_SELECT_ALL = 112 } capng_select_t; typedef enum { CAPNG_FAIL=-1, CAPNG_NONE, CAPNG_PARTIAL, CAPNG_FULL } capng_results_t; typedef enum { CAPNG_PRINT_STDOUT, CAPNG_PRINT_BUFFER } capng_print_t; typedef enum { CAPNG_NO_FLAG=0, CAPNG_DROP_SUPP_GRP=1, CAPNG_CLEAR_BOUNDING=2, CAPNG_INIT_SUPP_GRP=4, CAPNG_CLEAR_AMBIENT=8 } capng_flags_t; #define CAPNG_UNSET_ROOTID -1 #define CAPNG_SUPPORTS_AMBIENT 1 // These functions manipulate process capabilities void capng_clear(capng_select_t set); void capng_fill(capng_select_t set); void capng_setpid(int pid); int capng_get_caps_process(void) __wur; int capng_update(capng_act_t action, capng_type_t type,unsigned int capability); int capng_updatev(capng_act_t action, capng_type_t type, unsigned int capability, ...); // These functions apply the capabilities previously setup to a process int capng_apply(capng_select_t set) __wur; int capng_lock(void) __wur; int capng_change_id(int uid, int gid, capng_flags_t flag) __wur; // These functions are used for file based capabilities int capng_get_rootid(void); int capng_set_rootid(int rootid); int capng_get_caps_fd(int fd) __wur; int capng_apply_caps_fd(int fd) __wur; // These functions check capability bits capng_results_t capng_have_capabilities(capng_select_t set); capng_results_t capng_have_permitted_capabilities(void); int capng_have_capability(capng_type_t which, unsigned int capability); // These functions printout capabilities char *capng_print_caps_numeric(capng_print_t where, capng_select_t set) __attr_dealloc_free; char *capng_print_caps_text(capng_print_t where, capng_type_t which) __attr_dealloc_free; // These functions convert between numeric and text string int capng_name_to_capability(const char *name); const char *capng_capability_to_name(unsigned int capability); // These function should be used when you suspect a third party library // may use libcap-ng also and want to make sure it doesn't alter something // important. Otherwise you shouldn't need to call these. void capng_restore_state(void **state); void *capng_save_state(void); #ifdef __cplusplus } #endif #endif libcap-ng-0.8.5/src/captab.h000066400000000000000000000055341460532736100155700ustar00rootroot00000000000000/* captab.h -- * Copyright 2009,2011-14,2020 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ _S(CAP_CHOWN, "chown" ) _S(CAP_DAC_OVERRIDE, "dac_override" ) _S(CAP_DAC_READ_SEARCH, "dac_read_search" ) _S(CAP_FOWNER, "fowner" ) _S(CAP_FSETID, "fsetid" ) _S(CAP_KILL, "kill" ) _S(CAP_SETGID, "setgid" ) _S(CAP_SETUID, "setuid" ) _S(CAP_SETPCAP, "setpcap" ) _S(CAP_LINUX_IMMUTABLE, "linux_immutable" ) _S(CAP_NET_BIND_SERVICE, "net_bind_service" ) _S(CAP_NET_BROADCAST, "net_broadcast" ) _S(CAP_NET_ADMIN, "net_admin" ) _S(CAP_NET_RAW, "net_raw" ) _S(CAP_IPC_LOCK, "ipc_lock" ) _S(CAP_IPC_OWNER, "ipc_owner" ) _S(CAP_SYS_MODULE, "sys_module" ) _S(CAP_SYS_RAWIO, "sys_rawio" ) _S(CAP_SYS_CHROOT, "sys_chroot" ) _S(CAP_SYS_PTRACE, "sys_ptrace" ) _S(CAP_SYS_PACCT, "sys_pacct" ) _S(CAP_SYS_ADMIN, "sys_admin" ) _S(CAP_SYS_BOOT, "sys_boot" ) _S(CAP_SYS_NICE, "sys_nice" ) _S(CAP_SYS_RESOURCE, "sys_resource" ) _S(CAP_SYS_TIME, "sys_time" ) _S(CAP_SYS_TTY_CONFIG, "sys_tty_config" ) _S(CAP_MKNOD, "mknod" ) _S(CAP_LEASE, "lease" ) _S(CAP_AUDIT_WRITE, "audit_write" ) _S(CAP_AUDIT_CONTROL, "audit_control" ) #ifdef CAP_SETFCAP _S(CAP_SETFCAP, "setfcap" ) #endif #ifdef CAP_MAC_OVERRIDE _S(CAP_MAC_OVERRIDE, "mac_override" ) #endif #ifdef CAP_MAC_ADMIN _S(CAP_MAC_ADMIN, "mac_admin" ) #endif #ifdef CAP_SYSLOG _S(CAP_SYSLOG, "syslog" ) #endif #if defined(CAP_EPOLLWAKEUP) && defined(CAP_BLOCK_SUSPEND) #error "Both CAP_EPOLLWAKEUP and CAP_BLOCK_SUSPEND are defined" #endif #ifdef CAP_EPOLLWAKEUP _S(CAP_EPOLLWAKEUP, "epollwakeup" ) #endif #ifdef CAP_WAKE_ALARM _S(CAP_WAKE_ALARM, "wake_alarm" ) #endif #ifdef CAP_BLOCK_SUSPEND _S(CAP_BLOCK_SUSPEND, "block_suspend" ) #endif #ifdef CAP_AUDIT_READ _S(CAP_AUDIT_READ, "audit_read" ) #endif #ifdef CAP_PERFMON _S(CAP_PERFMON, "perfmon" ) #endif #ifdef CAP_BPF _S(CAP_BPF, "bpf" ) #endif #ifdef CAP_CHECKPOINT_RESTORE _S(CAP_CHECKPOINT_RESTORE, "checkpoint_restore") #endif libcap-ng-0.8.5/src/libcap-ng.pc.in000066400000000000000000000003401460532736100167400ustar00rootroot00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: libcap-ng Description: An alternate POSIX capabilities library. Version: @VERSION@ Libs: -L${libdir} -lcap-ng Cflags: -I${includedir} libcap-ng-0.8.5/src/libdrop_ambient.c000066400000000000000000000020511460532736100174520ustar00rootroot00000000000000/* libdrop_ambient.c -- * Copyright 2020 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include void __attribute__ ((constructor)) init(void) { prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); } libcap-ng-0.8.5/src/lookup_table.c000066400000000000000000000067131460532736100170110ustar00rootroot00000000000000/* lookup_table.c -- * Copyright 2009, 2013 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include #include #include #include #include // free #define hidden __attribute__ ((visibility ("hidden"))) extern unsigned int last_cap hidden; #undef cap_valid #define cap_valid(x) ((x) <= last_cap) struct transtab { int value; int offset; }; #define MSGSTRFIELD(line) MSGSTRFIELD1(line) #define MSGSTRFIELD1(line) str##line /* To create the following tables in a DSO-friendly way we split them in two separate variables: a long string which is created by concatenating all strings referenced in the table and the table itself, which uses offsets instead of string pointers. To do this without increasing the maintenance burden we use a lot of preprocessor magic. All the maintainer has to do is to add a new entry to the included file and recompile. */ static const union captab_msgstr_t { struct { #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof (s)]; #include "captab.h" #undef _S }; char str[0]; } captab_msgstr = { { #define _S(n, s) s, #include "captab.h" #undef _S } }; static const struct transtab captab[] = { #define _S(n, s) { n, offsetof(union captab_msgstr_t, \ MSGSTRFIELD(__LINE__)) }, #include "captab.h" #undef _S }; #define CAP_NG_CAPABILITY_NAMES (sizeof(captab)/sizeof(captab[0])) static int capng_lookup_name(const struct transtab *table, const char *tabstr, size_t length, const char *name) { size_t i; for (i = 0; i < length; i++) { if (!strcasecmp(tabstr + table[i].offset, name)) return table[i].value; } return -1; } static const char *capng_lookup_number(const struct transtab *table, const char *tabstr, size_t length, int number) { size_t i; for (i = 0; i < length; i++) { if (table[i].value == number) return tabstr + table[i].offset; } return NULL; } int capng_name_to_capability(const char *name) { return capng_lookup_name(captab, captab_msgstr.str, CAP_NG_CAPABILITY_NAMES, name); } static char *ptr2 = NULL; const char *capng_capability_to_name(unsigned int capability) { const char *ptr; if (!cap_valid(capability)) return NULL; ptr = capng_lookup_number(captab, captab_msgstr.str, CAP_NG_CAPABILITY_NAMES, capability); if (ptr == NULL) { // This leaks memory, but should almost never be used free(ptr2); if (asprintf(&ptr2, "cap_%u", capability) < 0) ptr = NULL; else ptr = ptr2; } return ptr; } libcap-ng-0.8.5/src/test/000077500000000000000000000000001460532736100151355ustar00rootroot00000000000000libcap-ng-0.8.5/src/test/Makefile.am000066400000000000000000000021311460532736100171660ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,10,15 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING.LIB. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # AM_CPPFLAGS = -I${top_srcdir} -I${top_srcdir}/src check_PROGRAMS = lib_test thread_test TESTS = $(check_PROGRAMS) lib_test_LDADD = ${top_builddir}/src/libcap-ng.la thread_test_LDADD = ${top_builddir}/src/libcap-ng.la -lpthread libcap-ng-0.8.5/src/test/lib_test.c000066400000000000000000000123311460532736100171060ustar00rootroot00000000000000/* lib_test.c -- simple libcap-ng test suite * Copyright 2009,2012-13 Red Hat Inc. * All Rights Reserved. * * 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 program; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include "../cap-ng.h" #include #include #include #include #include #include static unsigned int get_last_cap(void) { int fd; fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY); if (fd == -1) { return CAP_LAST_CAP; } else { char buf[8]; int num = read(fd, buf, sizeof(buf)); if (num > 0) { errno = 0; unsigned int val = strtoul(buf, NULL, 10); if (errno == 0) return val; } close(fd); } return CAP_LAST_CAP; } int main(void) { int rc; unsigned int i, len, last = get_last_cap(); char *text; void *saved; puts("Doing basic bit tests..."); capng_clear(CAPNG_SELECT_BOTH); if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_NONE) { puts("Failed clearing capabilities"); abort(); } saved = capng_save_state(); capng_fill(CAPNG_SELECT_BOTH); if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_FULL) { puts("Failed filling capabilities"); abort(); } // Need to detect if version 1 or 2 capabilities text = capng_print_caps_numeric(CAPNG_PRINT_BUFFER, CAPNG_SELECT_CAPS); len = strlen(text); free(text); if (len < 80 && last > 30) // The kernel & headers are mismatched last = 30; // Now test that restore still works capng_restore_state(&saved); if (capng_have_capabilities(CAPNG_SELECT_BOTH) != CAPNG_NONE) { puts("Failed restoring capabilities"); abort(); } printf("Doing advanced bit tests for %d capabilities...\n", last); for (i=0; i<=last; i++) { const char *name; capng_clear(CAPNG_SELECT_BOTH); rc = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); if (rc) { puts("Failed update test 1"); abort(); } rc = capng_have_capability(CAPNG_EFFECTIVE, i); if (rc == 0) { puts("Failed have capability test 1"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_CAPS); abort(); } if(capng_have_capabilities(CAPNG_SELECT_CAPS)!=CAPNG_PARTIAL){ puts("Failed have capabilities test 1"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_CAPS); abort(); } #if CAP_LAST_CAP > 31 rc = capng_update(CAPNG_ADD, CAPNG_BOUNDING_SET, i); if (rc) { puts("Failed bset update test 2"); abort(); } rc = capng_have_capability(CAPNG_BOUNDING_SET, i); if (rc == 0) { puts("Failed bset have capability test 2"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_BOTH); abort(); } if(capng_have_capabilities(CAPNG_SELECT_BOUNDS)!=CAPNG_PARTIAL){ puts("Failed bset have capabilities test 2"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_BOTH); abort(); } #endif text=capng_print_caps_text(CAPNG_PRINT_BUFFER, CAPNG_EFFECTIVE); if (text == NULL) { puts("Failed getting print text to buffer"); abort(); } name = capng_capability_to_name(i); if (name == NULL) { printf("Failed converting capability %d to name\n", i); abort(); } if (strcmp(text, name)) { puts("Failed print text comparison"); printf("%s != %s\n", text, name); abort(); } free(text); // Now make sure the mask part is working capng_fill(CAPNG_SELECT_BOTH); rc = capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, i); if (rc) { puts("Failed update test 3"); abort(); } // Should be partial if(capng_have_capabilities(CAPNG_SELECT_CAPS)!=CAPNG_PARTIAL){ puts("Failed have capabilities test 3"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_CAPS); abort(); } // Add back the bit and should be full capabilities rc = capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); if (rc) { puts("Failed update test 4"); abort(); } if (capng_have_capabilities(CAPNG_SELECT_CAPS) != CAPNG_FULL){ puts("Failed have capabilities test 4"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_CAPS); abort(); } } // Now test the updatev function capng_clear(CAPNG_SELECT_BOTH); rc = capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE, CAP_CHOWN, CAP_FOWNER, CAP_KILL, -1); if (rc) { puts("Failed updatev test"); abort(); } rc = capng_have_capability(CAPNG_EFFECTIVE, CAP_CHOWN) && capng_have_capability(CAPNG_EFFECTIVE, CAP_FOWNER) && capng_have_capability(CAPNG_EFFECTIVE, CAP_KILL); if (rc == 0) { puts("Failed have updatev capability test"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_CAPS); abort(); } return EXIT_SUCCESS; } libcap-ng-0.8.5/src/test/thread_test.c000066400000000000000000000027001460532736100176060ustar00rootroot00000000000000#include "config.h" #include #include #include #include //#define DEBUG 1 pthread_t thread1, thread2; void *thread1_main(void *arg) { capng_fill(CAPNG_SELECT_BOTH); #ifdef DEBUG printf("thread1 filled capabilities\n"); #endif sleep(2); if (capng_have_capabilities(CAPNG_SELECT_CAPS) < CAPNG_FULL) { printf("Capabilities missing when there should be some\n"); exit(1); } #ifdef DEBUG printf("SUCCESS: Full capabilities reported\n"); #endif return NULL; } void *thread2_main(void *arg) { sleep(1); #ifdef DEBUG printf("thread2 getting capabilities\n"); #endif if (capng_get_caps_process()) { printf("Unable to get process capabilities"); exit(1); } if (capng_have_capabilities(CAPNG_SELECT_CAPS) != CAPNG_NONE) { printf("Detected capabilities when there should not be any\n"); exit(1); } capng_clear(CAPNG_SELECT_BOTH); #ifdef DEBUG printf("SUCCESS: No capabilities reported\n"); #endif return NULL; } int main(void) { // This test must be run as root which naturally has all capabilities // set. So, we need to clear the capabilities so that we can see if // the test works. capng_clear(CAPNG_SELECT_CAPS); if (capng_apply(CAPNG_SELECT_CAPS)) { printf("Clearing capabilities failed"); return 1; } printf("Testing thread separation of capabilities\n"); pthread_create(&thread1, NULL, thread1_main, NULL); pthread_create(&thread2, NULL, thread2_main, NULL); sleep(3); return 0; } libcap-ng-0.8.5/utils/000077500000000000000000000000001460532736100145275ustar00rootroot00000000000000libcap-ng-0.8.5/utils/Makefile.am000066400000000000000000000025171460532736100165700ustar00rootroot00000000000000# Makefile.am -- # Copyright 2009,2015 Red Hat Inc. # All Rights Reserved. # # 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 program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor # Boston, MA 02110-1335, USA. # # Authors: # Steve Grubb # CONFIG_CLEAN_FILES = *.loT *.rej *.orig AUTOMAKE_OPTIONS = no-dependencies EXTRA_DIST = $(man_MANS) AM_CPPFLAGS = -I${top_srcdir} -I${top_srcdir}/src LDADD = ${top_builddir}/src/libcap-ng.la AM_CFLAGS = -W -Wall -Wshadow ${WFLAGS} -Wundef -D_GNU_SOURCE bin_PROGRAMS = pscap netcap filecap captest man_MANS = pscap.8 netcap.8 filecap.8 captest.8 pscap_SOURCES = pscap.c netcap_SOURCES = netcap.c proc-llist.c proc-llist.h filecap_SOURCES = filecap.c captest_SOURCES = captest.c libcap-ng-0.8.5/utils/captest.8000066400000000000000000000040251460532736100162640ustar00rootroot00000000000000.TH CAPTEST: "8" "Sept 2020" "Red Hat" "System Administration Utilities" .SH NAME captest \- a program to demonstrate capabilities .SH SYNOPSIS .B captest [ \-\-ambient \-\-drop-all | \-\-drop-caps | \-\-id ] [ \-\-init-grp ] [ \-\-lock ] [ \-\-text ] .SH DESCRIPTION \fBcaptest\fP is a program that demonstrates and prints out the current process capabilities. Each option prints the same report. It will output current capabilities. then it will try to access /etc/shadow directly to show if that can be done. Then it creates a child process that attempts to read /etc/shadow and outputs the results of that. Then it outputs the capabilities that a child process would have. You can also apply file system capabilities to this program to study how they work. For example, filecap /usr/bin/captest chown. Then run captest as a normal user. Another interesting test is to make captest suid root so that you can see what the interaction is between root's credentials and capabilities. For example, chmod 4755 /usr/bin/captest. When run as a normal user, the program will see if privilege escalation is possible. But do not leave this app setuid root after you are don testing so that an attacker cannot take advantage of it. .SH OPTIONS .TP .B \-\-ambient This attempts to add CAP_CHOWN ambient capability. .TP .B \-\-drop-all This drops all capabilities including ambient and clears the bounding set. .TP .B \-\-drop-caps This drops just traditional capabilities. .TP .B \-\-id This changes to uid and gid 99, drops supplemental groups, and clears the bounding set. .TP .B \-\-init-grp This changes to uid and gid 99 and then adds any supplemental groups that comes with that account. You would have add them prior to testing because by default there are no supplemental groups on account 99. .TP .B \-\-text This option outputs the effective capabilities in text rather than numerically. .TP .B \-\-lock This prevents the ability for child processes to regain privileges if the uid is 0. .SH "SEE ALSO" .BR filecap (8), .BR capabilities (7) .SH AUTHOR Steve Grubb libcap-ng-0.8.5/utils/captest.c000066400000000000000000000173661460532736100163530ustar00rootroot00000000000000/* * captest.c - A program that demonstrates and outputs capabilities * Copyright (c) 2009, 2013, 2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb * */ #include "config.h" #include #include #include #include #include #include #include #include #ifdef HAVE_LINUX_SECUREBITS_H #include #endif /* children can't get caps back */ #ifndef SECURE_NOROOT #define SECURE_NOROOT 0 #endif #ifndef SECURE_NOROOT_LOCKED #define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */ #endif /* Setuid apps run by uid 0 don't get caps back */ #ifndef SECURE_NO_SETUID_FIXUP #define SECURE_NO_SETUID_FIXUP 2 #endif #ifndef SECURE_NO_SETUID_FIXUP_LOCKED #define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */ #endif static int text = 0, no_child = 0, lock = 0, ambient = 0; static void report(void) { int rc, escalated = 0, need_comma = 0; uid_t uid, euid, suid; gid_t gid, egid, sgid; // Refresh what we have for capabilities if (capng_get_caps_process()) { printf("Error getting capabilities\n"); exit(1); } // Check user credentials getresuid(&uid, &euid, &suid); getresgid(&gid, &egid, &sgid); if (no_child) { if ((uid != euid && uid != 0) || capng_have_capability(CAPNG_EFFECTIVE, CAP_SETUID)) { printf("Attempting to regain root..."); setuid(0); getresuid(&uid, &euid, &suid); if (uid == 0) { printf("SUCCESS - PRIVILEGE ESCALATION POSSIBLE\n"); setgid(0); getresgid(&gid, &egid, &sgid); escalated = 1; } else printf("FAILED\n"); } printf("Child "); } printf("User credentials uid:%u euid:%u suid:%u\n", uid, euid, suid); if (no_child) printf("Child "); printf("Group credentials gid:%u egid:%u sgid:%u\n", gid, egid, sgid); if (uid != euid || gid != egid) printf("Note: app has mismatching credentials!!\n"); // Check capabilities if (text) { if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) { if (no_child) printf("Child capabilities: none\n"); else printf("Current capabilities: none\n"); } else { if (no_child) printf("Child "); printf("Effective: "); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_EFFECTIVE); printf("\n"); if (no_child) printf("Child "); printf("Permitted: "); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_PERMITTED); printf("\n"); if (no_child) printf("Child "); printf("Inheritable: "); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_INHERITABLE); printf("\n"); if (no_child) printf("Child "); printf("Bounding Set: "); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_BOUNDING_SET); printf("\n"); if (no_child) printf("Child "); printf("Ambient: "); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_AMBIENT); printf("\n"); } } else { if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) { if (no_child) printf("Child capabilities: none\n"); else printf("Current capabilities: none\n"); } else { if (no_child) printf("Child capabilities:\n"); capng_print_caps_numeric(CAPNG_PRINT_STDOUT, CAPNG_SELECT_ALL); } } // Now check securebits flags #ifdef PR_SET_SECUREBITS if (no_child) printf("Child "); printf("securebits flags: "); rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NOROOT); if (rc & (1 << SECURE_NOROOT)) { printf("NOROOT"); need_comma = 1; } rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NOROOT_LOCKED); if (rc & (1 << SECURE_NOROOT_LOCKED)) { if (need_comma) printf(", "); printf("NOROOT_LOCKED"); need_comma = 1; } rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NO_SETUID_FIXUP); if (rc & (1 << SECURE_NO_SETUID_FIXUP)) { if (need_comma) printf(", "); printf("NO_SETUID_FIXUP"); need_comma = 1; } rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NO_SETUID_FIXUP_LOCKED); if (rc & (1 << SECURE_NO_SETUID_FIXUP_LOCKED)) { if (need_comma) printf(", "); printf("NO_SETUID_FIXUP_LOCKED"); need_comma = 1; } if (need_comma == 0) printf("none"); printf("\n"); #endif // Now do child process checks if (no_child == 0 || escalated) { printf("Attempting direct access to shadow..."); if (access("/etc/shadow", R_OK) == 0) printf("SUCCESS\n"); else printf("FAILED (%s)\n", strerror(errno)); } if (no_child == 0) { printf("Attempting to access shadow by child process..."); rc = system("cat /etc/shadow > /dev/null 2>&1"); if (rc == 0) printf("SUCCESS\n"); else printf("FAILED\n"); if (text) system("/usr/bin/captest --no-child --text"); else system("/usr/bin/captest --no-child"); } } static void usage(void) { printf("usage: captest [ --ambient | --drop-all | --drop-caps | --id | --init-grp ] [ --lock ] [ --text ]\n"); } int main(int argc, char *argv[]) { int which = 0, i; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--text") == 0) text = 1; else if (strcmp(argv[i], "--no-child") == 0) no_child = 1; else if (strcmp(argv[i], "--lock") == 0) lock = 1; else if (strcmp(argv[i], "--drop-all") == 0) which = 1; else if (strcmp(argv[i], "--drop-caps") == 0) which = 2; else if (strcmp(argv[i], "--id") == 0) which = 3; else if (strcmp(argv[i], "--init-grp") == 0) which = 4; else if (strcmp(argv[i], "--ambient") == 0) ambient = 1; else { usage(); return 0; } } switch (which) { case 1: capng_clear(CAPNG_SELECT_ALL); if (lock) { if (capng_lock()) { fprintf(stderr, "capng_lock failed\n"); return 1; } } if (capng_apply(CAPNG_SELECT_ALL)) { fprintf(stderr, "capng_apply failed\n"); return 1; } report(); break; case 2: capng_clear(CAPNG_SELECT_CAPS); if (ambient) capng_update(CAPNG_ADD, CAPNG_AMBIENT, CAP_CHOWN); if (lock) { if (capng_lock()) { fprintf(stderr, "capng_lock failed\n"); return 1; } } if (ambient) { if (capng_apply( CAPNG_SELECT_CAPS|CAPNG_SELECT_AMBIENT)) { fprintf(stderr, "capng_apply failed\n"); return 1; } } else { if (capng_apply(CAPNG_SELECT_CAPS)) { fprintf(stderr, "capng_apply failed\n"); return 1; } } report(); break; case 3: case 4: { int rc; capng_clear(CAPNG_SELECT_BOTH); capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_CHOWN); if (ambient) capng_update(CAPNG_ADD, CAPNG_INHERITABLE|CAPNG_AMBIENT, CAP_CHOWN); if (which == 4) rc = capng_change_id(99, 99, CAPNG_INIT_SUPP_GRP | CAPNG_CLEAR_BOUNDING); else rc = capng_change_id(99, 99, CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING); if (rc < 0) { printf("Error changing uid: %d\n", rc); capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_EFFECTIVE); printf("\n"); return 1; } printf("Keeping CAP_CHOWN to show capabilities across uid change.\n"); report(); } break; case 0: if (lock) { if (capng_lock()) { fprintf(stderr, "capng_lock failed\n"); return 1; } } report(); break; } return 0; } libcap-ng-0.8.5/utils/filecap.8000066400000000000000000000031531460532736100162250ustar00rootroot00000000000000.TH FILECAP: "8" "Aug 2018" "Red Hat" "System Administration Utilities" .SH NAME filecap \- a program to see capabilities .SH SYNOPSIS .B filecap [ \-a | \-d | /dir | /dir/file [cap1 cap2 ...] ] .SH DESCRIPTION \fBfilecap\fP is a program that prints out a report of programs with file based capabilities. If a file is not in the report or there is no report at all, no capabilities were found. For expedience, the default is to check only the directories in the PATH environmental variable. If the \-a command line option is given, then all directories will be checked. If a directory is passed, it will recursively check that directory. If a path to a file is given, it will only check that file. If a file is given followed by capabilities, then the capabilities are written to the file. .SH OPTIONS .TP .B \-a This tells the program to show all capabilities starting from the / directory. Normally the PATH environmental variable is used to show you capabilities on files you are likely to execute. .TP .B \-d This dumps all capabilities for reference. .SH "EXAMPLES" .nf To check file capabilities in $PATH: .B filecap To check file capabilities of whole system: .B filecap -a To check file capabilities recursively in a directory: .B filecap /usr To check file capabilities of a specific program: .B filecap /bin/passwd To list all possible capabilities: .B filecap -d To set a file capability on a specific program: .B filecap /bin/ping net_raw net_admin To remove file capabilities on a specific program: .B filecap /bin/ping none .fi .SH "SEE ALSO" .BR pscap (8), .BR netcap (8), .BR capabilities (7). .SH AUTHOR Steve Grubb libcap-ng-0.8.5/utils/filecap.c000066400000000000000000000127651460532736100163110ustar00rootroot00000000000000/* * filecap.c - A program that lists running processes with capabilities * Copyright (c) 2009-10,2012,2017,2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include #include #include #include #include "cap-ng.h" #include #include #ifndef FTW_CONTINUE #define FTW_CONTINUE 0 #endif static int show_all = 0, header = 0, capabilities = 0, cremove = 0; static int single_file = 0; static void usage(void) { fprintf(stderr, "usage: filecap [-a | -d | /dir | /dir/file [cap1 cap2 ...] ]\n"); exit(1); } // Returns 1 on error and 0 otherwise static int check_file(const char *fpath, const struct stat *sb, int typeflag_unused __attribute__ ((unused)), struct FTW *s_unused __attribute__ ((unused))) { int ret = FTW_CONTINUE; if (S_ISREG(sb->st_mode) == 0) return ret; int fd = open(fpath, O_RDONLY|O_CLOEXEC); if (fd >= 0) { capng_results_t rc; int permitted = 0; capng_clear(CAPNG_SELECT_BOTH); if (capng_get_caps_fd(fd) < 0 && errno != ENODATA) { fprintf(stderr, "Unable to get capabilities of %s: %s\n", fpath, strerror(errno)); if (single_file) ret = 1; } rc = capng_have_capabilities(CAPNG_SELECT_CAPS); if (rc == CAPNG_NONE) { permitted = 1; rc = capng_have_permitted_capabilities(); } if (rc > CAPNG_NONE) { if (header == 0) { header = 1; printf("%-9s %-20s capabilities rootid\n", "set", "file"); } int rootid = capng_get_rootid(); printf("%s %s ", permitted ? "permitted" : "effective", fpath); if (rc == CAPNG_FULL) printf("full"); else capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_PERMITTED); if (rootid != CAPNG_UNSET_ROOTID) printf(" %d", rootid); printf("\n"); } close(fd); } return ret; } // Use cases: // filecap // filecap -a // filecap /path/dir // filecap /path/file // filecap /path/file capability1 capability2 capability 3 ... // int main(int argc, char *argv[]) { #if CAP_LAST_CAP < 31 || !defined (VFS_CAP_U32) || \ (!defined (HAVE_ATTR_XATTR_H) && !defined(HAVE_SYS_XATTR_H)) fprintf(stderr, "File based capabilities are not supported\n"); #else char *path_env, *path = NULL, *dir = NULL; struct stat sbuf; int nftw_flags = FTW_PHYS; int i, rc = 0; if (argc >1) { for (i=1; i= 0) { if (path == NULL) usage(); capng_update(CAPNG_ADD, CAPNG_PERMITTED|CAPNG_EFFECTIVE, cap); capabilities = 1; } else if (strcmp("none", argv[i]) == 0) { capng_clear(CAPNG_SELECT_BOTH); capabilities = 1; cremove = 1; } else { fprintf(stderr, "Unrecognized capability.\n"); usage(); } } } } if (path == NULL && dir == NULL && show_all == 0) { path_env = getenv("PATH"); if (path_env != NULL) { path = strdup(path_env); if (!path) return 1; for (dir=strtok(path,":"); dir!=NULL; dir=strtok(NULL,":")) { nftw(dir, check_file, 1024, nftw_flags); } free(path); } } else if (path == NULL && dir == NULL && show_all == 1) { // Find files nftw("/", check_file, 1024, nftw_flags); } else if (dir) { // Print out the dir nftw(dir, check_file, 1024, nftw_flags); }else if (path && capabilities == 0) { // Print out specific file single_file = 1; rc = check_file(path, &sbuf, 0, NULL); } else if (path && capabilities == 1) { // Write capabilities to file int fd = open(path, O_WRONLY|O_NOFOLLOW|O_CLOEXEC); if (fd < 0) { fprintf(stderr, "Could not open %s for writing (%s)\n", path, strerror(errno)); return 1; } if (capng_apply_caps_fd(fd) < 0) { fprintf(stderr, "Could not set capabilities on %s: %s\n", path, strerror(errno)); rc = 1; } close(fd); } #endif return rc; } libcap-ng-0.8.5/utils/netcap.8000066400000000000000000000016031460532736100160720ustar00rootroot00000000000000.TH NETCAP: "8" "Sept 2020" "Red Hat" "System Administration Utilities" .SH NAME netcap \- a program to see capabilities .SH SYNOPSIS .B netcap .SH DESCRIPTION \fBnetcap\fP is a program that prints out a report of process capabilities. If the application is using tcp, udp, raw, or packet family of sockets AND has any capabilities, it will be in the report. If the process has partial capabilities, it is further examined to see if it has an open-ended bounding set. If this is found to be true, a '+' symbol is added. If the process has ambient capabilities, a '@' symbols is added. Some directories in the /proc file system are readonly by root. The program will try to access and report what it can. But if nothing comes out, try changing to the root user and re-run this program. .SH "SEE ALSO" .BR pscap (8), .BR filecap (8), .BR capabilities (7), .BR netstat (8). .SH AUTHOR Steve Grubb libcap-ng-0.8.5/utils/netcap.c000066400000000000000000000263721460532736100161570ustar00rootroot00000000000000/* * netcap.c - A program that lists network apps with capabilities * Copyright (c) 2009-10,2012,2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb * * The /proc/net/tcp|udp|raw parsing code was borrowed from netstat.c */ #include "config.h" #include #include #include #include #include #include #include #include #include "cap-ng.h" #include "proc-llist.h" static llist l; static int perm_warn = 0, header = 0, last_uid = -1; static char *tacct = NULL; static void usage(void) { fprintf(stderr, "usage: netcap\n"); exit(1); } static int collect_process_info(void) { DIR *d, *f; struct dirent *ent; d = opendir("/proc"); if (d == NULL) { fprintf(stderr, "Can't open /proc: %s\n", strerror(errno)); return 1; } while (( ent = readdir(d) )) { FILE *sf; int pid, ppid; capng_results_t caps; char buf[100]; char *tmp, cmd[16], state; char *text = NULL, *bounds = NULL, *ambient = NULL; int fd, len, euid = -1; // Skip non-process dir entries if(*ent->d_name<'0' || *ent->d_name>'9') continue; errno = 0; pid = strtol(ent->d_name, NULL, 10); if (errno) continue; // Parse up the stat file for the proc snprintf(buf, 32, "/proc/%d/stat", pid); fd = open(buf, O_RDONLY|O_CLOEXEC, 0); if (fd < 0) continue; len = read(fd, buf, sizeof buf - 1); close(fd); if (len < 40) continue; buf[len] = 0; tmp = strrchr(buf, ')'); if (tmp) *tmp = 0; else continue; memset(cmd, 0, sizeof(cmd)); sscanf(buf, "%d (%15c", &ppid, cmd); sscanf(tmp+2, "%c %d", &state, &ppid); // Skip kthreads if (pid == 2 || ppid == 2) continue; // now get the capabilities capng_clear(CAPNG_SELECT_ALL); capng_setpid(pid); if (capng_get_caps_process()) continue; caps = capng_have_capabilities(CAPNG_SELECT_CAPS); if (caps <= CAPNG_NONE) continue; if (caps == CAPNG_FULL) { text = strdup("full"); if (!text) { fprintf(stderr, "Out of memory\n"); continue; } } else { text = capng_print_caps_text(CAPNG_PRINT_BUFFER, CAPNG_PERMITTED); if (text == NULL) { fprintf(stderr, "Out of memory doing pid %d\n", pid); continue; } } // Get the effective uid snprintf(buf, 32, "/proc/%d/status", pid); sf = fopen(buf, "rte"); if (sf == NULL) euid = 0; else { int line = 0; __fsetlocking(sf, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), sf)) { if (line == 0) { line++; continue; } if (memcmp(buf, "Uid:", 4) == 0) { int id; sscanf(buf, "Uid: %d %d", &id, &euid); break; } } fclose(sf); } caps = capng_have_capabilities(CAPNG_SELECT_AMBIENT); if (caps > CAPNG_NONE) ambient = strdup("@"); else ambient = strdup(""); if (!ambient) { fprintf(stderr, "Out of memory\n"); free(text); continue; } // Now record the bounding set information caps = capng_have_capabilities(CAPNG_SELECT_BOUNDS); if (caps > CAPNG_NONE) bounds = strdup("+"); else bounds = strdup(""); if (!bounds) { fprintf(stderr, "Out of memory\n"); free(text); free(ambient); continue; } // Now lets get the inodes each process has open snprintf(buf, 32, "/proc/%d/fd", pid); f = opendir(buf); if (f == NULL) { if (errno == EACCES) { if (perm_warn == 0) { fprintf(stderr, "You may need to be root to " "get a full report\n"); perm_warn = 1; } } else fprintf(stderr, "Can't open %s: %s\n", buf, strerror(errno)); free(text); free(bounds); free(ambient); continue; } // For each file in the fd dir... while (( ent = readdir(f) )) { char line[256], ln[256], *s, *e; unsigned long inode; lnode node; int llen; if (ent->d_name[0] == '.') continue; snprintf(ln, 256, "%s/%s", buf, ent->d_name); if ((llen = readlink(ln, line, sizeof(line)-1)) < 0) continue; line[llen] = 0; // Only look at the socket entries if (memcmp(line, "socket:", 7) == 0) { // Type 1 sockets s = strchr(line+7, '['); if (s == NULL) continue; s++; e = strchr(s, ']'); if (e == NULL) continue; *e = 0; } else if (memcmp(line, "[0000]:", 7) == 0) { // Type 2 sockets s = line + 8; } else continue; errno = 0; inode = strtoul(s, NULL, 10); if (errno) continue; node.ppid = ppid; node.pid = pid; node.uid = euid; node.cmd = strdup(cmd); node.inode = inode; node.capabilities = strdup(text); node.bounds = strdup(bounds); node.ambient = strdup(ambient); if (node.cmd && node.capabilities && node.bounds && node.ambient) // We make one entry for each socket inode list_append(&l, &node); else { free(node.cmd); free(node.capabilities); free(node.bounds); free(node.ambient); } } closedir(f); free(text); free(bounds); free(ambient); } closedir(d); return 0; } static void report_finding(unsigned int port, const char *type, const char *ifc) { struct passwd *p; lnode *n = list_get_cur(&l); // And print out anything with capabilities if (header == 0) { printf("%-5s %-5s %-10s %-16s %-8s %-6s %s\n", "ppid", "pid", "acct", "command", "type", "port", "capabilities"); header = 1; } if (n->uid == 0) { // Take short cut for this one tacct = "root"; last_uid = 0; } else if (last_uid != (int)n->uid) { // Only look up if name changed p = getpwuid(n->uid); last_uid = n->uid; if (p) tacct = p->pw_name; // If not taking this branch, use last val } if (tacct) { printf("%-5d %-5d %-10s", n->ppid, n->pid, tacct); } else printf("%-5d %-5d %-10d", n->ppid, n->pid, last_uid); printf(" %-16s %-8s", n->cmd, type); if (ifc) printf(" %-6s", ifc); else printf(" %-6u", port); printf(" %s %s%s\n", n->capabilities, n->ambient, n->bounds); } static void read_tcp(const char *proc, const char *type) { int line = 0; FILE *f; char buf[256]; unsigned long rxq, txq, time_len, retr, inode; unsigned int local_port, rem_port, state, timer_run; int d, uid, timeout; char rem_addr[128], local_addr[128], more[512]; f = fopen(proc, "rte"); if (f == NULL) { if (errno != ENOENT) fprintf(stderr, "Can't open %s: %s\n", proc, strerror(errno)); return; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } more[0] = 0; sscanf(buf, "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X " "%lX:%lX %X:%lX %lX %d %d %lu %511s\n", &d, local_addr, &local_port, rem_addr, &rem_port, &state, &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (list_find_inode(&l, inode)) report_finding(local_port, type, NULL); } fclose(f); } static void read_udp(const char *proc, const char *type) { int line = 0; FILE *f; char buf[256]; unsigned long rxq, txq, time_len, retr, inode; unsigned int local_port, rem_port, state, timer_run; int d, uid, timeout; char rem_addr[128], local_addr[128], more[512]; f = fopen(proc, "rte"); if (f == NULL) { if (errno != ENOENT) fprintf(stderr, "Can't open %s: %s\n", proc, strerror(errno)); return; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } more[0] = 0; sscanf(buf, "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X " "%lX:%lX %X:%lX %lX %d %d %lu %511s\n", &d, local_addr, &local_port, rem_addr, &rem_port, &state, &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (list_find_inode(&l, inode)) report_finding(local_port, type, NULL); } fclose(f); } static void read_raw(const char *proc, const char *type) { int line = 0; FILE *f; char buf[256]; unsigned long rxq, txq, time_len, retr, inode; unsigned int local_port, rem_port, state, timer_run; int d, uid, timeout; char rem_addr[128], local_addr[128], more[512]; f = fopen(proc, "rte"); if (f == NULL) { if (errno != ENOENT) fprintf(stderr, "Can't open %s: %s\n", proc, strerror(errno)); return; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } more[0] = 0; sscanf(buf, "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X " "%lX:%lX %X:%lX %lX %d %d %lu %511s\n", &d, local_addr, &local_port, rem_addr, &rem_port, &state, &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (list_find_inode(&l, inode)) report_finding(0, type, NULL); } fclose(f); } // Caller must have buffer > 16 bytes static void get_interface(unsigned int iface, char *ifc) { unsigned int line = 0; FILE *f; char buf[256], more[256]; // Terminate the interface in case of error *ifc = 0; // Increment the interface number since header is 2 lines long iface++; f = fopen("/proc/net/dev", "rte"); if (f == NULL) { if (errno != ENOENT) fprintf(stderr, "Can't open /proc/net/dev: %s\n", strerror(errno)); return; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == iface) { char *c; sscanf(buf, "%16s: %255s\n", ifc, more); c = strchr(ifc, ':'); if (c) *c = 0; fclose(f); return; } line++; } fclose(f); } static void read_packet(void) { int line = 0; FILE *f; char buf[256]; unsigned long sk, inode; unsigned int ref_cnt, type, proto, iface, r, rmem, uid; char more[256], ifc[32]; f = fopen("/proc/net/packet", "rte"); if (f == NULL) { if (errno != ENOENT) fprintf(stderr, "Can't open /proc/net/packet: %s\n", strerror(errno)); return; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } more[0] = 0; sscanf(buf, "%lX %u %u %X %u %u %u %u %lu %255s\n", &sk, &ref_cnt, &type, &proto, &iface, &r, &rmem, &uid, &inode, more); get_interface(iface, ifc); if (list_find_inode(&l, inode)) report_finding(0, "pkt", ifc); } fclose(f); } int main(int argc, char __attribute__((unused)) *argv[]) { if (argc > 1) { fputs("Too many arguments\n", stderr); usage(); } list_create(&l); collect_process_info(); // Now we check the tcp socket list... read_tcp("/proc/net/tcp", "tcp"); read_tcp("/proc/net/tcp6", "tcp6"); // Next udp sockets... read_udp("/proc/net/udp", "udp"); read_udp("/proc/net/udp6", "udp6"); read_udp("/proc/net/udplite", "udplite"); read_udp("/proc/net/udplite6", "udplite6"); // Next, raw sockets... read_raw("/proc/net/raw", "raw"); read_raw("/proc/net/raw6", "raw6"); // And last, read packet sockets read_packet(); // Could also do icmp,netlink,unix list_clear(&l); return 0; } libcap-ng-0.8.5/utils/proc-llist.c000066400000000000000000000043121460532736100167630ustar00rootroot00000000000000/* * proc-llist.c - Minimal linked list library * Copyright (c) 2009, 2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include #include #include "proc-llist.h" void list_create(llist *l) { l->head = NULL; l->cur = NULL; l->cnt = 0; } void list_append(llist *l, lnode *node) { lnode* newnode; if (node == NULL || l == NULL) return; newnode = malloc(sizeof(lnode)); if (newnode == NULL) return; newnode->ppid = node->ppid; newnode->pid = node->pid; newnode->uid = node->uid; newnode->inode = node->inode; // Take custody of the memory newnode->cmd = node->cmd; newnode->capabilities = node->capabilities; newnode->bounds = node->bounds; newnode->ambient = node->ambient; newnode->next = NULL; // if we are at top, fix this up if (l->head == NULL) l->head = newnode; else // Otherwise add pointer to newnode l->cur->next = newnode; // make newnode current l->cur = newnode; l->cnt++; } void list_clear(llist* l) { lnode* nextnode; register lnode* cur; cur = l->head; while (cur) { nextnode=cur->next; free(cur->cmd); free(cur->capabilities); free(cur->bounds); free(cur->ambient); free(cur); cur=nextnode; } l->head = NULL; l->cur = NULL; l->cnt = 0; } lnode *list_find_inode(llist *l, unsigned long i) { register lnode* cur; cur = l->head; /* start at the beginning */ while (cur) { if (cur->inode == i) { l->cur = cur; return cur; } else cur = cur->next; } return NULL; } libcap-ng-0.8.5/utils/proc-llist.h000066400000000000000000000037121460532736100167730ustar00rootroot00000000000000/* * proc-llist.h * Copyright (c) 2009, 2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #ifndef PROC_HEADER #define PROC_HEADER #include "config.h" #include /* Ensure types in _lnode are defined on all systems */ /* This is the node of the linked list. Any data elements that are per * record goes here. */ typedef struct _lnode{ pid_t ppid; // parent process ID pid_t pid; // process ID uid_t uid; // user ID char *cmd; // command run by user unsigned long inode; // inode of socket char *capabilities; // Text of partial capabilities char *bounds; // Text for bounding set char *ambient; // Text for ambient set struct _lnode* next; // Next node pointer } lnode; /* This is the linked list head. Only data elements that are 1 per * event goes here. */ typedef struct { lnode *head; // List head lnode *cur; // Pointer to current node unsigned int cnt; // How many items in this list } llist; void list_create(llist *l); static inline lnode *list_get_cur(llist *l) { return l->cur; } void list_append(llist *l, lnode *node); void list_clear(llist* l); /* Given a message type, find the matching node */ lnode *list_find_inode(llist *l, unsigned long i); #endif libcap-ng-0.8.5/utils/pscap.8000066400000000000000000000021151460532736100157250ustar00rootroot00000000000000.TH PSCAP: "8" "Sept 2020" "Red Hat" "System Administration Utilities" .SH NAME pscap \- a program to see capabilities .SH SYNOPSIS .B pscap [ \-a | \-p pid ] .SH DESCRIPTION \fBpscap\fP is a program that prints out a report of process capabilities. If the application has any capabilities, it will be in the report with the exception of init. By giving the \-a command line option, init will be included, too. By giving a pid with the \-p command line option, only the process specified with the pid is reported. If a process is not in the report, it has dropped all capabilities. If the process has partial capabilities, it is further examined to see if it has an open-ended bounding set. If this is found to be true, a '+' symbol is added. If the process has ambient capabilities, a '@' symbols is added. The command name in the output may be followed by an asterisk mark (*). This mark denotes processes which run in child user namespaces (relative to the user namespace of pscap itself). .SH "SEE ALSO" .BR netcap (8), .BR filecap (8), .BR capabilities (7), .BR ps (8). .SH AUTHOR Steve Grubb libcap-ng-0.8.5/utils/pscap.c000066400000000000000000000136071460532736100160100ustar00rootroot00000000000000/* * pscap.c - A program that lists running processes with capabilities * Copyright (c) 2009,2012,2020 Red Hat Inc. * All Rights Reserved. * * This software may be freely redistributed and/or modified under the * terms of the GNU General Public License as published by the Free * Software Foundation; either version 2, 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor * Boston, MA 02110-1335, USA. * * Authors: * Steve Grubb */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include "cap-ng.h" #define CMD_LEN 16 #define USERNS_MARK_LEN 2 static void usage(void) { fprintf(stderr, "usage: pscap [-a|-p pid]\n"); exit(1); } /* * Precise recursive checks for parent-child relation between namespaces * using ioctl() were avoided, because there didn't seem to be any case when * we may dereference the namespace symlink in /proc/PID/ns for processes in * user namespaces other than the current or child ones. Thus, the check just * tries to dereference the link and checks that it does not point to the * current NS. */ static bool in_child_userns(int pid) { char ns_file_path[32]; struct stat statbuf; ino_t own_ns_inode; dev_t own_ns_dev; if (stat("/proc/self/ns/user", &statbuf) < 0) return false; own_ns_inode = statbuf.st_ino; own_ns_dev = statbuf.st_dev; snprintf(ns_file_path, 32, "/proc/%d/ns/user", pid); if (stat(ns_file_path, &statbuf) < 0) return false; return statbuf.st_ino != own_ns_inode || statbuf.st_dev != own_ns_dev; } int main(int argc, char *argv[]) { char *endptr = NULL; DIR *d; struct dirent *ent; int header = 0, show_all = 0, caps; pid_t our_pid = getpid(); pid_t target_pid = 0; if (argc > 3) { fputs("Too many arguments\n", stderr); usage(); } if (argc == 2) { if (strcmp(argv[1], "-a") == 0) show_all = 1; else usage(); } else if (argc == 3) { if (strcmp(argv[1], "-p") == 0) { errno = 0; target_pid = strtol(argv[2], &endptr, 10); if (errno) { fprintf(stderr, "Can't read pid: %s\n", argv[2]); return 1; } if ((endptr == argv[2]) || (*endptr != '\0') || !target_pid) { fprintf(stderr, "Invalid pid argument: %s\n", argv[2]); return 1; } if (target_pid == 1) show_all = 1; } else usage(); } d = opendir("/proc"); if (d == NULL) { fprintf(stderr, "Can't open /proc: %s\n", strerror(errno)); return 1; } while (( ent = readdir(d) )) { int pid, ppid, uid = -1, euid = -1; char buf[100]; char *tmp, cmd[CMD_LEN + USERNS_MARK_LEN], state, *name = NULL; int fd, len; struct passwd *p; // Skip non-process dir entries if(*ent->d_name<'0' || *ent->d_name>'9') continue; errno = 0; pid = strtol(ent->d_name, NULL, 10); if (errno) continue; if (target_pid && (pid != target_pid)) continue; /* Skip our pid so we aren't listed */ if (pid == our_pid) continue; // Parse up the stat file for the proc snprintf(buf, 32, "/proc/%d/stat", pid); fd = open(buf, O_RDONLY|O_CLOEXEC, 0); if (fd < 0) continue; len = read(fd, buf, sizeof buf - 1); close(fd); if (len < 40) continue; buf[len] = 0; tmp = strrchr(buf, ')'); if (tmp) *tmp = 0; else continue; memset(cmd, 0, sizeof(cmd)); sscanf(buf, "%d (%15c", &ppid, cmd); sscanf(tmp+2, "%c %d", &state, &ppid); // Skip kthreads if (pid == 2 || ppid == 2) continue; if (!show_all && pid == 1) continue; // now get the capabilities capng_clear(CAPNG_SELECT_ALL); capng_setpid(pid); if (capng_get_caps_process()) continue; // And print out anything with capabilities caps = capng_have_capabilities(CAPNG_SELECT_CAPS); if (caps > CAPNG_NONE) { // Get the effective uid FILE *f; int line; snprintf(buf, 32, "/proc/%d/status", pid); f = fopen(buf, "rte"); if (f == NULL) euid = 0; else { line = 0; __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } if (memcmp(buf, "Uid:", 4) == 0) { int id; sscanf(buf, "Uid: %d %d", &id, &euid); break; } } fclose(f); } if (header == 0) { printf("%-5s %-5s %-10s %-18s %s\n", "ppid", "pid", "uid", "command", "capabilities"); header = 1; } if (euid == 0) { // Take short cut for this one name = "root"; uid = 0; } else if (euid != uid) { // Only look up if name changed p = getpwuid(euid); uid = euid; if (p) name = p->pw_name; // If not taking this branch, use last val } if (in_child_userns(pid)) strcat(cmd, " *"); if (name) { printf("%-5d %-5d %-10s %-18s ", ppid, pid, name, cmd); } else printf("%-5d %-5d %-10d %-18s ", ppid, pid, uid, cmd); if (caps == CAPNG_PARTIAL) { capng_print_caps_text(CAPNG_PRINT_STDOUT, CAPNG_PERMITTED); if (capng_have_capabilities( CAPNG_SELECT_AMBIENT) > CAPNG_NONE) printf(" @"); if (capng_have_capabilities(CAPNG_SELECT_BOUNDS) > CAPNG_NONE) printf(" +"); printf("\n"); } else { printf("full"); if (capng_have_capabilities( CAPNG_SELECT_AMBIENT) > CAPNG_NONE) printf(" @"); if (capng_have_capabilities(CAPNG_SELECT_BOUNDS) > CAPNG_NONE) printf(" +"); printf("\n"); } } } closedir(d); return 0; }