├── ACKNOWLEDGEMENTS
├── AUTHORS
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── Makefile
├── README
├── cache_timer.c
├── cache_timer.h
├── ecr.sh
├── instructions.c
├── instructions.h
├── main.c
├── main.h
├── memory_timer.c
├── memory_timer.h
├── mod_ioctl.c
├── mod_ioctl.h
├── nop_timer.c
├── nop_timer.h
├── wall_timer.c
└── wall_timer.h
/ACKNOWLEDGEMENTS:
--------------------------------------------------------------------------------
1 | The authors would like to thank Jacob Torrey for his thoughts and aid in this project.
2 |
3 | This research is based upon work supported in part by the Office of the Director of National Intelligence (ODNI), Intelligence Advanced Research Projects Activity (IARPA). The views and conclusions contained herein are those of the authors and should not be interpreted as necessarily representing the official policies, either expressed or implied, of ODNI, IARPA, or the U.S. Government. The U.S. Government is authorized to reproduce and distribute reprints for governmental purposes notwithstanding any copyright annotation therein.
4 |
--------------------------------------------------------------------------------
/AUTHORS:
--------------------------------------------------------------------------------
1 | Tomasz Tuzel
2 | Mark Bridgman
3 | Joshua Zepf
4 | Jacob Torrey
5 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at tuzelt@ainfosec.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Contributors License Agreement
2 | To get started, sign the Contributor License Agreement.
3 |
4 | ## Forking
5 |
6 | Before you can contribute, you must fork the repo that you wish to contribute to. GitHub already has great documentation on how to fork a repo, make a modification, and create a pull request. If you are not familiar with this process, please read the following before continuing:
7 |
8 | https://help.github.com/articles/fork-a-repo
9 | https://help.github.com/articles/using-pull-requests
10 |
11 | ## Questions
12 |
13 | This project provides multiple methods for asking other community members questions about the project. All questions are welcome, but prior to asking a question, please ensure that existing documentation and / or previous Q&As have not already answered your question prior to submission.
14 |
15 | **Bug Tracking / RFCs / Feature Requests / Questions:**
16 | https://github.com/ainfosec/ecr_toolkit/issues
17 |
18 | ## Feature Requests
19 |
20 | Feature requests are always welcome as they help to drive the creation of future roadmaps by the community. Please submit all feature requests to the GitHub Bug Tracker with “Feature Request:” prepended to the message’s title. Doing so provides a means for community members to filter and comment on the feature requests, ask questions, and provide input.
21 |
22 | **Feature Requests:**
23 | https://github.com/ainfosec/ecr_toolkit/issues
24 |
25 | If a feature request already exists, please use the existing feature request message, and add a “+1” comment to show your desire for the feature. Features requests with higher community support are more likely to be added to the roadmap.
26 |
27 | ## Reporting Issues / Vulnerabilities
28 |
29 | All issues and vulnerabilities should be submitted to the following issue tracker:
30 |
31 | **Bug Tracking:**
32 | https://github.com/ainfosec/ecr_toolkit/issues
33 |
34 | When submitting an issue, please include the following documentation:
35 |
36 | - Title
37 | - Brief description of the issue
38 | - Instructions for how to reproduce the issue
39 | - Version information (or commit hash)
40 | - Environment information (distribution, supporting library information, compiler, hardware)
41 | - Console logs (if applicable)
42 | - Backtrace logs (if applicable)
43 |
44 | ## Request for Comments
45 |
46 | Prior to submitting changes to the project, a Request for Comments (RFC) is highly advised. An RFC provides the community with an opportunity to provide feedback prior to submission. The submitter should view the RFC as a chance to gain community support early, reducing the likelihood of push back during submission.
47 |
48 | All RFCs should be submitted to the project’s GitHub Bug Tracker with “RFC:” prepended to the message’s title. Doing so provides a means for community members to filter and comment on the RFC, ask questions, and provide input.
49 |
50 | **RFCs:**
51 | https://github.com/ainfosec/ecr_toolkit/issues
52 |
53 | Although an RFC can take on any format, please ensure that an RFC includes the following:
54 |
55 | - Title
56 | - TL;DR (summary)
57 | - Detailed description
58 | - Proposed API changes (if applicable)
59 | - Proposed documentation changes (if applicable)
60 | - Proposed testing changes (if applicable)
61 |
62 | Although an RFC should contain as much information as possible to reduce the number of questions likely to be asked by the community, it should also be as brief as possible. RFCs that are too long will likely see limited community involvement, resulting in a higher risk of push back during the submission of any proposed changes.
63 |
64 | ## Pull Requests / Commit Message Format
65 |
66 | Anyone can submit a pull request to the project. All pull requests must satisfy the requirements defined in the governance documentation. If the pull request includes source code, tests for the source code must also be included (usually in the form of unit tests). All CI tests must also return without error, demonstrating that not only does the source code contain the proper tests to validate it’s implementation, but these tests succeed.
67 |
68 | This project uses Astyle to format the source code, and the project provides a configuration file (astyle.conf) that should be used. Prior to submitting changes to the project, all source code should be run through Astyle to verify that the proper formatting rules are applied. If this is not done, Travis CI will fail when the PR is submitted. To format the code run "make astyle" and "make astyle_clean" when complete.
69 |
70 | Clang Tidy and Google's Sanitizers are used to perform static / dynamic analysis. If a PR fails because of these checks you can run them locally, or review the Travis CI log. To perform the Clang Tidy checks on Linux, install [bear](https://github.com/rizsotto/Bear) and run:
71 |
72 | ```
73 | make clean
74 | STATIC_ANALYSIS_ENABLED=true bear make
75 | make tidy
76 | ```
77 |
78 | To perform the Google Sanitizer checks run:
79 |
80 | ```
81 | make clean
82 | DYNAMIC_ANALYSIS_ENABLED=true make
83 | make test
84 | ```
85 |
86 | All whitespace should be removed as well. Use "git diff --check" to ensure the PR does not contain added whitespace. Doxygen is also used by this project. Prior to submitting a PR, please run "make doxygen" and "make doxygen_clean" to ensure there are no errors with respect to the documentation.
87 |
88 | Prior to submitting a PR, please rebase your git history to a single commit. To do this:
89 |
90 | ```
91 | git fetch
92 | git rebase -i /master
93 | "pick -> r" for the top commit
94 | "pick -> f" for the rest of the commits
95 | ```
96 |
97 | All commits to the project should have the following commit format. The information used in this commit format provides the community with a means to create the project’s changelog, as well as identify what each commit addresses.
98 |
99 | - Title (50 characters or less)
100 | - Empty line
101 | - Description (wrapped to 72 characters per line)
102 | - Empty line
103 | - \[ISSUE\]: link (if applicable)
104 | - \[RFC\]: link (if applicable)
105 | - Empty line
106 | - Sign-off
107 |
108 | For Example:
109 |
110 | ```
111 | Added new API XYZ
112 |
113 | The library was missing the ability to do blah, and as a
114 | result, blah was not possible. The following commit adds
115 | XYZ to the project providing the ability to:
116 |
117 | - Do blah blah
118 | - And blah
119 | - As well as blah
120 |
121 | The following blah was added to the unit tests to validate
122 | that this new API works as intended.
123 |
124 | [RFC]: link
125 |
126 | Signed-off-by: John Smith
127 | ```
128 |
129 | ## API Documentation
130 |
131 | All functions should be documented using Doxygen style comment blocks. Each comment block should define the following:
132 |
133 | - All parameters and their expected values
134 | - Return value (if applicable)
135 | - Error cases
136 | - Notes (if applicable)
137 | - Code Example (if applicable)
138 |
139 | Since comments are kept to a minimum, the function’s comment block is a great place to put notes about the function’s implementation. For more information about Doxygen, please see:
140 |
141 | [Doxygen Manual](http://www.stack.nl/~dimitri/doxygen/)
142 |
143 | ## Tagged Releases
144 |
145 | Major tags will be managed by the community, and signify milestones in the project’s roadmap. It is up to the community members to maintain major tags, and there are currently no guarantees on the life-time of a tagged version of this project.
146 |
147 | At any point in time, a community member can request a minor tagged version of the project via an RFC. Minor tags are managed by the community member(s) that requested the tag. The request should include:
148 |
149 | - What to tag (git hash)
150 | - Who’s requesting the tag
151 | - How long the community member(s) plan to maintain the tag
152 | - Level of testing being applied to the tag
153 | - Supported configurations
154 |
155 | Since more than one organization could be basing their products off of this project, minor tags provide an organization with a means to baseline the project, and provide others with some of the maintenance and stability the organization is likely to provide. The alternative would be the organization maintains an internal, stable fork of the project, that others cannot benefit from.
156 |
157 | ## Roles and Responsibilities
158 |
159 | The following defines the different roles that make up this project, as well as defines the responsibilities for each of these roles. These roles are based on a meritocratic, community owned governance model, and as such, all changes to the project are communicated, and voted on prior to approval. The project as a whole is owned by the community members, and it’s vision and goals are defined by the community.
160 |
161 | This governance model strives to provide all members of the community with a voice, and has no special requirements for community members to be given a vote on changes to the project. In the event community consensus cannot be reached, a small subset of community members have the authority to vote on a resolution.
162 |
163 | ### Users
164 |
165 | Users are community members who use the project. There are no requirements to be a user of this project. Users can contribute back to the project in multiple ways including:
166 |
167 | - Using the project
168 | - Advertising the project to others (word-of-mouth, website links, reviews, etc…)
169 | - Reporting issues
170 | - Providing new feature requests
171 | - Providing financial support
172 | - Providing moral support (a thank you goes a long way)
173 |
174 | ### Contributors
175 |
176 | Contributors are community members who contribute to the project in one way or another. There are no special requirements to how much a community member must contribute to be a contributor. Since anyone can be a contributor, this project does, however, provide a set of guidelines that defines what, when and how contributions are accepted by the community. In addition to the responsibilities of a user, a contributor contributes changes to the project. This includes (but is not limited to, and nor does it require):
177 |
178 | - New features
179 | - Bug fixes
180 | - Design documentation
181 | - Installation documentation
182 | - Usage documentation
183 | - API documentation
184 | - Graphics / web design
185 | - Commenting on RFCs
186 | - Voting on changes to the project
187 | - General feedback
188 |
189 | All contributions made to the project should be socialized with the community to increase the chances of acceptance. For new features, this is down via a Request For Comments (RFC). Bug fixes should be reported using the issue tracker, and provided as a patch (or pull request) to the community.
190 |
191 | All contributors have the right to vote on changes to the project prior to their acceptance, providing an equal opportunity for everyone’s voice to be heard. This includes providing feedback on RFCs, solutions to currently tracked bugs, and all other changes that are made to the project. Since there are no special requirements to be a contributor, everyone’s opinion must be considered, regardless of their involvement in the project. If a general consensus cannot be made, the project owners are responsible for voting to resolve the conflict.
192 |
193 | ### Owners
194 |
195 | Owners are community members who own the project. Owners have little responsibility over and above a contributor. Their primary goal is to commit changes to the project, and resolve conflicts. The responsibilities of an owner includes that of a user and contributor, while also including:
196 |
197 | - Committing changes to the project
198 | - Voting on changes to the project when a consensus cannot be reached among the contributors.
199 | - Voting on the addition / removal of owners
200 |
201 | Although owners have the ability to commit a change to the project, they are still required to get consensus from the contributors. Ideally, the entire community would agree on a specific change, unanimously. In the case where an agreement cannot be made, the owners are responsible for voting to resolve the conflict.
202 |
203 | ## Conflict Resolution
204 |
205 | This project uses a form of lazy consensus to approve and disapprove changes to the project. When a change is proposed (could be a pull request, RFC, etc…), 48 hours should be provided to give all community members an opportunity to comment if so desired, prior to an owner committing the change to the project.
206 |
207 | Community members approve a change to the project by stating +1, or not stating anything at all (i.e. silence is consent). If a community member objects (i.e. -1), that community member is responsible for providing an alternative approach to the proposed change. Failure to do so is equivalent to a +1 and shall be treated as such (i.e. simply objecting is not tolerated, and will be treated as consent).
208 |
209 | It is likely that at some point, the community will not agree on a proposed change (i.e. unanimous consent was not reached, or in other words, at least one contributor provides a -1 with an alternative approach). If this should occur, the owners of the project are responsible for resolving the conflict. Once a vote is taken up by the owners, 48 hours should be provide for all of the owners to have an opportunity to comment if so desired. Like the community, owners use a lazy consensus model. A +1 or silence states approval by an owner. The difference is, a -1 by an owner during the vote to resolve a conflict does _not_ need to be accompanied by a proposed alternative. Majority vote wins. In the event of a tie, no action is taken, and the community must work towards an alternate solution that does not result in a tie.
210 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Environmental Characterization & Response
2 | # Copyright (C) 2018 Assured Information Security, Inc.
3 | #
4 | # This program is free software; you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation; either version 2 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License along
15 | # with this program; if not, write to the Free Software Foundation, Inc.,
16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 |
18 | ccflags-y := -std=gnu99
19 |
20 | obj-m += ecr.o
21 | ecr-y := main.o instructions.o wall_timer.o nop_timer.o cache_timer.o memory_timer.o
22 |
23 | UNAME_R = $(shell uname -r)
24 | KDIR ?= /lib/modules/$(UNAME_R)/build
25 |
26 | all:
27 | gcc -o mod_ioctl mod_ioctl.c
28 | make -C $(KDIR) M=$(PWD) modules
29 |
30 | clean:
31 | rm mod_ioctl
32 | make -C $(KDIR) M=$(PWD) clean
33 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | Copyright (C) 2018 Assured Information Security, Inc
2 |
3 | See our whitepaper here: https://dfrws.org/sites/default/files/session-files/paper_who_watches_the_watcher_detecting_hypervisor_introspection_from_unprivileged_guests.pdf
4 |
5 | # ** Build/run from domU **
6 |
7 | # Build the ECR package.
8 | make
9 |
10 | # Run the module without any options to obtain timing for each instruction, sans the non-temporal move operations.
11 | sudo ./ecr.sh ecr.ko
12 |
13 | # To test the non-temporal instructions, enable kernel memory allocation. See README.libvmi in the ecr_hypervisor Github repository to use xen-emulate-response to cause variations in move instruction timing:
14 | sudo ./ecr.sh ecr.ko -k
15 |
16 | # For memory timing introspection:
17 | sudo ./ecr.sh ecr.ko -m
18 | # Memory will be allocated. If using the the AIS modified hypervisor (see Note 3), use the address above for the xen-emulate-response address to emulate. Once it is running, press enter to obtain timing metrics. Repeat as necessary for different address offsets to observe variations across pages.
19 |
20 | # For cache timing introspection:
21 | sudo ./ecr.sh ecr.ko -l
22 | # Memory will be allocated. If using the the AIS modified hypervisor (see Note 3), use the address above for the map-addr address to map. Once it is running, press enter to obtain timing metrics. Repeat as necessary for different address offsets to observe variations and/or ensure granularity.
23 |
24 | # Note 1: Kernel memory allocation may fail. In this case, simply rerun the module.
25 | # Note 2: To obtain output in a CSV file, run with -c
26 | # Note 3: To obtain the AIS modified hypervisor: git clone --recurse-submodules https://github.com/ainfosec/ecr_hypervisor.git
27 |
--------------------------------------------------------------------------------
/cache_timer.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include "cache_timer.h"
21 |
22 | unsigned long inline cache_timing_probe(char *addr) {
23 | volatile unsigned long time;
24 |
25 | asm volatile(
26 | "mfence\n"
27 | "rdtsc\n"
28 | "movl %%eax, %%esi\n"
29 | "movl (%1), %%eax\n"
30 | "lfence\n"
31 | "rdtsc\n"
32 | "subl %%esi, %%eax\n"
33 | : "=a"(time)
34 | : "c"(addr)
35 | : "%esi", "%edx"
36 | );
37 | return time;
38 | }
39 |
40 | void inline cache_timing_flush(char *addr) {
41 | asm volatile(
42 | "clflush 0(%0)\n"
43 | "mfence\n"
44 | :
45 | : "c"(addr)
46 | : "%rax"
47 | );
48 | }
49 |
50 | int cache_timing_handler(int offset) {
51 | unsigned long t;
52 | int i, avg = 0;
53 | extern uintptr_t *vmem_aligned;
54 |
55 | for ( i = 0; i < COUNT; i++ ) {
56 | cache_timing_flush((char *) (vmem_aligned+offset));
57 | msleep(1);
58 | t = cache_timing_probe((char *) (vmem_aligned+offset));
59 | avg += t;
60 | }
61 |
62 | avg /= COUNT;
63 |
64 | pr_info("Average clock cycles at address 0x%lx (offset: 0x%x): %d\n", \
65 | (unsigned long) vmem_aligned+offset, offset, avg);
66 |
67 | return 0;
68 | }
69 |
70 | int cache_timing_start(void) {
71 | cache_timing_handler(0);
72 | cache_timing_handler(64);
73 | cache_timing_handler(128);
74 | cache_timing_handler(256);
75 | cache_timing_handler(4096);
76 |
77 | return 0;
78 | }
79 |
--------------------------------------------------------------------------------
/cache_timer.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #ifndef CACHE_TIMER_H
21 | #define CACHE_TIMER_H
22 |
23 | #include
24 | #include
25 |
26 | #define COUNT 100
27 |
28 | int cache_timing_handler(int offset);
29 | int cache_timing_start(void);
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/ecr.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Environmental Characterization & Response
4 | # Copyright (C) 2018 Assured Information Security, Inc.
5 | #
6 | # This program is free software; you can redistribute it and/or modify
7 | # it under the terms of the GNU General Public License as published by
8 | # the Free Software Foundation; either version 2 of the License, or
9 | # (at your option) any later version.
10 | #
11 | # This program is distributed in the hope that it will be useful,
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | # GNU General Public License for more details.
15 | #
16 | # You should have received a copy of the GNU General Public License along
17 | # with this program; if not, write to the Free Software Foundation, Inc.,
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 |
20 | # ** Ensure that we are running as root **
21 | if ! [ $(id -u) = 0 ]; then
22 | echo "Run this script as root"
23 | exit 1
24 | fi
25 |
26 | standard_instructions=(XOR
27 | CPUID
28 | RDTSC
29 | RDTSCP
30 | INVD
31 | CLTS
32 | INB
33 | OUTB
34 | INVLPG
35 | INVPCID
36 | MOV_FROM_CR8
37 | MOV_TO_CR0
38 | MOV_TO_CR3
39 | MOV_TO_CR4
40 | MOV_TO_CR8
41 | MOV_DR0
42 | MOV_DR1
43 | MOV_DR2
44 | MOV_DR3
45 | MOV_DR4
46 | MOV_DR5
47 | MOV_DR6
48 | MOV_DR7
49 | RDMSR
50 | RDPMC
51 | RDRAND
52 | RDSEED
53 | WBINVD
54 | WRMSR
55 | MOVNTDQA
56 | MOVNTDQ)
57 |
58 | flush_instructions=(CLFLUSH
59 | SFENCE
60 | MFENCE)
61 |
62 | # ** Timing measurement functions **
63 | function wall_parse {
64 | dm=$(dmesg | grep -A100000000 `dmesg | grep Inserting | tail -1 \
65 | | awk -F '[][]' '{print $2}'` | grep $1 | tail -1)
66 | parsed=${dm##*: }
67 | echo $parsed
68 | }
69 |
70 | function nop_parse {
71 | dm=$(dmesg | grep -A100000000 `dmesg | grep Inserting | tail -1 \
72 | | awk -F '[][]' '{print $2}'` | grep $1 -A 1 | tail -1)
73 | parsed=${dm##* }
74 | echo $parsed
75 | }
76 |
77 | function cache_parse {
78 | val=$1
79 | dm=$(dmesg | grep -A100000000 `dmesg | grep Inserting | tail -1 \
80 | | awk -F '[][]' '{print $2}'` | grep "Average clock cycles" | grep -w $val)
81 | parsed=${dm##* }
82 | echo $parsed
83 | }
84 |
85 | function memory_parse {
86 | val=$1
87 | dm=$(dmesg | grep -A100000000 `dmesg | grep Inserting | tail -1 \
88 | | awk -F '[][]' '{print $2}'` | grep "access page $val")
89 | parsed=${dm##* }
90 | echo $parsed
91 | }
92 |
93 | function average {
94 | temp=("$@")
95 | avg=0 ; count=0
96 | for i in "${temp[@]}"; do
97 | avg=$((avg + i))
98 | count=$((count + 1))
99 | done
100 | if [ "$count" -eq "0" ] ; then
101 | count=1
102 | fi
103 | avg=$((avg / count))
104 | echo $avg
105 | }
106 |
107 | function variance {
108 | avg=$1
109 | arr=("${@:2}")
110 | var=0 ; count=0
111 | for i in "${arr[@]}"; do
112 | count=$((count + 1))
113 | calc=$((i - avg))
114 | calc=$((calc * calc))
115 | var=$((var + calc))
116 | done
117 | if [ "$count" -eq "0" ] ; then
118 | count=1
119 | fi
120 | var=$((var / count))
121 | echo $var
122 | }
123 |
124 | function evaluate_exiting {
125 | temp1=$1
126 | temp2=$2
127 | wall_high=$((wall_avg_CPUID - temp1))
128 | wall_low=$((temp1 - wall_avg_XOR))
129 | nop_high=$((nop_avg_CPUID - temp2))
130 | nop_low=$((temp2 - nop_avg_XOR))
131 | exiting=""
132 | if [[ $wall_high -lt $wall_low ]] && [[ $nop_high -lt $nop_low ]] ; then
133 | exiting="X"
134 | elif [[ $wall_high -lt $wall_low ]] || [[ $nop_high -lt $nop_low ]] ; then
135 | exiting="?"
136 | fi
137 | echo $exiting
138 | }
139 |
140 | # ** Trap handling functions **
141 | function ctrl_c() {
142 | printf "Ctrl+C received, exiting and cleaning up...\n" $1
143 | sudo rmmod $1
144 | exit 0
145 | }
146 |
147 | function finish() {
148 | sudo rmmod $1 2>/dev/null
149 | exit 0
150 | }
151 |
152 | # ** Command line argument parsing **
153 | # This will use the GNU Enhanced getopt if available, and will otherwise fall
154 | # back on the original getopt
155 | program_name=`basename $0`
156 |
157 | echo "$program_name: Starting..."
158 |
159 | # Trap on errors, interrupts, and exits
160 | trap "echo $program_name: error encountered: aborted" ERR
161 | trap "ctrl_c $1" INT
162 | trap "finish $1" EXIT
163 |
164 | # Define options
165 | opts_short=kcflmh
166 | opts_long=kmalloc,csv,flush,cache,memory,help
167 |
168 | help_short="Usage: $program_name [options]
169 | When none of the below options are used, core instruction timing is run by default.
170 | Options:
171 | -k allocate kernel memory (required for non-temporal instructions)
172 | -c output in csv file
173 | -f run buffer flush instructions (will not run standard instruction timing)
174 | -l run cache timing operations
175 | -m run memory timing operations
176 | -h show this help message"
177 |
178 | help_long="Usage: $program_name [options]
179 | When none of the below options are used, core instruction timing is run by default.
180 | Options:
181 | -k, --kmalloc allocate kernel memory (required for non-temporal instructions)
182 | -c, --csv output in csv file
183 | -f, --flush run buffer flush instructions (will not run standard instruction timing)
184 | -l, --cache run cache timing operations
185 | -m, --memory run memory timing operations
186 | -h, --help show this help message"
187 |
188 | # Check if GNU Enhanced getopt is available
189 | enhanced_getopt_available=
190 | if getopt -T >/dev/null; then :
191 | else
192 | if [ $? -eq 4 ]; then
193 | enhanced_getopt_available=yes
194 | fi
195 | fi
196 |
197 | # Run getopt (running it in an if-statement so the trap does not interfere)
198 | if [ -n "$enhanced_getopt_available" ]; then
199 | # Use GNU enhanced getopt
200 | if ! getopt --name "$program_name" --long $opts_long --options $opts_short -- "$@" >/dev/null; then
201 | echo "$program_name: error (use -h or --help for help)" >&2
202 | exit 2
203 | fi
204 | args=`getopt --name "$program_name" --long $opts_long --options $opts_short -- "$@"`
205 | else
206 | # Use original getopt
207 | if ! getopt $opts_short "$@" >/dev/null; then
208 | echo "$program_name: error (use -h for help)" >&2
209 | exit 2
210 | fi
211 | args=`getopt $opts_short "$@"`
212 | fi
213 | eval set -- $args
214 |
215 | # Process parsed options
216 | while [ $# -gt 0 ]; do
217 | case "$1" in
218 | -k | --kmalloc) kmalloc_enable=yes;;
219 | -c | --csv) csv_enable=yes;;
220 | -f | --flush) flush_enable=yes;
221 | kmalloc_enable=yes;;
222 | -l | --cache) cache_enable=yes;
223 | kmalloc_enable=yes;;
224 | -m | --memory) memory_enable=yes;
225 | kmalloc_enable=yes;;
226 | -h | --help) if [ -n "$enhanced_getopt_available" ]
227 | then echo "$help_long";
228 | else echo "$help_short";
229 | fi; exit 0;;
230 | --) shift; break;; # end of options
231 | esac
232 | shift
233 | done
234 |
235 | sudo insmod $1
236 | if [ $? -eq 1 ]; then
237 | echo "$program_name: Invalid kernel object."
238 | exit 1
239 | fi
240 |
241 | # Allocate memory, if necessary
242 | if [ -n "$kmalloc_enable" ]; then
243 | echo "$program_name: Allocating memory..."
244 | sudo ./mod_ioctl 7 >/dev/null
245 | dm=$(dmesg | grep "Allocating memory" -A 1 | grep -o "0x[a-fA-F0-9]*" | tail -1)
246 | if [ -z "$dm" ]; then
247 | echo "$program_name: Memory allocation failed. Rerun the script."
248 | exit 1
249 | fi
250 | echo "$program_name: Memory has been allocated at kernel virtual address: $dm"
251 | read -p "$program_name: Press Enter to continue"
252 | fi
253 |
254 | # If running the buffer flushing instructions, set that as the array, and set the
255 | # appropriate option numbers for the ioctl to use
256 | if [ -n "$flush_enable" ]; then
257 | eval $(typeset -A -p flush_instructions|sed 's/ flush_instructions=/ instructions=/')
258 | ioctl_option_1="5"
259 | ioctl_option_2="6"
260 | else
261 | eval $(typeset -A -p standard_instructions|sed 's/ standard_instructions=/ instructions=/')
262 | ioctl_option_1="1"
263 | ioctl_option_2="2"
264 | fi
265 | declare -a instructions
266 |
267 | # ** Start of measurements **
268 | if [ -n "$cache_enable" ]; then
269 | echo "$program_name: Running cache timing operations..."
270 | sudo ./mod_ioctl 3 >/dev/null
271 | echo ""
272 | echo " Offset | Tick Count"
273 | echo " -------|-----------"
274 | declare -a address_offsets=("0x0" "0x40" "0x80" "0x100" "0x1000")
275 | for i in "${address_offsets[@]}"
276 | do
277 | cache_arr[$i]=$(cache_parse $i)
278 | if [ -n "$csv_enable" ]; then
279 | # ** Print results to csv file **
280 | printf "%s,%s\n" $i ${cache_arr[$i]} >> results.csv
281 | else
282 | # ** Print results table **
283 | printf " %7s |%11s\n" $i ${cache_arr[$i]}
284 | fi
285 | done
286 | sudo rmmod $1
287 | elif [ -n "$memory_enable" ]; then
288 | echo "$program_name: Running memory timing operations..."
289 | sudo ./mod_ioctl 4 >/dev/null
290 | echo ""
291 | echo " Offset | Tick Count"
292 | echo " -------|-----------"
293 | for i in {0..9}
294 | do
295 | memory_arr[$i]=$(memory_parse $i)
296 | if [ -n "$csv_enable" ]; then
297 | # ** Print results to csv file **
298 | printf "%s,%s\n" $i ${memory_arr[$i]} >> results.csv
299 | else
300 | # ** Print results table **
301 | printf " %7s |%11s\n" $i ${memory_arr[$i]}
302 | fi
303 | done
304 | sudo rmmod $1
305 | else
306 | echo "$program_name: Running timing..."
307 | for i in `seq 1 15` ; do
308 | sudo ./mod_ioctl $ioctl_option_1 >/dev/null
309 | for j in "${instructions[@]}"; do
310 | eval name="WALL_$j:"
311 | eval wall_arr_$j[$i]=$(wall_parse $name)
312 | done
313 |
314 | sudo ./mod_ioctl $ioctl_option_2 >/dev/null
315 | for j in "${instructions[@]}"; do
316 | eval name="NOP_$j:"
317 | eval nop_arr_$j[$i]=$(nop_parse $name)
318 | done
319 | done
320 | sudo rmmod $1
321 |
322 | for j in "${instructions[@]}"; do
323 | eval wall_arr=\${wall_arr_$j[@]}
324 | eval nop_arr=\${nop_arr_$j[@]}
325 | eval wall_avg_$j=$(average ${wall_arr[@]})
326 | eval nop_avg_$j=$(average ${nop_arr[@]})
327 | eval wall_avg=\$wall_avg_$j
328 | eval nop_avg=\$nop_avg_$j
329 | eval wall_var_$j=$(variance $wall_avg ${wall_arr[@]})
330 | eval nop_var_$j=$(variance $nop_avg ${nop_arr[@]})
331 | done
332 |
333 | for j in "${instructions[@]}"; do
334 | eval wall_avg=\$wall_avg_$j
335 | eval nop_avg=\$nop_avg_$j
336 | eval exit_flag_$j=$(evaluate_exiting $wall_avg $nop_avg)
337 | done
338 | exit_flag_INB="-"
339 | exit_flag_OUTB="-"
340 |
341 | echo "$program_name: Finished"
342 |
343 | if [ -n "$csv_enable" ]; then
344 | # ** Print results to csv file **
345 | if [ -f ./results.csv ] ; then
346 | rm results.csv
347 | fi
348 | for j in "${instructions[@]}"; do
349 | eval name=\$j
350 | eval wall_avg=\$wall_avg_$j
351 | eval nop_avg=\$nop_avg_$j
352 | eval wall_var=\$wall_var_$j
353 | eval nop_var=\$nop_var_$j
354 | printf "%s,%d,%d,%d,%d\n" $name $wall_avg $nop_avg $wall_var $nop_var >> results.csv
355 | done
356 | else
357 | # ** Print results table **
358 | echo ""
359 | echo " Instruction | Wall Time Avg | NOP Time Avg | Probable Exit"
360 | echo " ------------|---------------|--------------|--------------"
361 | for j in "${instructions[@]}"; do
362 | eval name=\$j
363 | eval wall_avg=\$wall_avg_$j
364 | eval nop_avg=\$nop_avg_$j
365 | eval exit_flag=\$exit_flag_$j
366 | printf " %12s |%14d |%13d |%14s\n" $name $wall_avg $nop_avg $exit_flag
367 | done
368 | fi
369 | fi
370 |
--------------------------------------------------------------------------------
/instructions.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include "instructions.h"
21 |
22 | // CPU feature check for virtual machine extensions
23 | void cpuid_test_support_f(void) {
24 | int cpuid_leaf = 1;
25 | int cpuid_ecx = 0;
26 |
27 | asm volatile (
28 | "cpuid\n\t"
29 | : "=c"(cpuid_ecx)
30 | : "a"(cpuid_leaf)
31 | : "%rbx", "%rdx"
32 | );
33 |
34 | if( (cpuid_ecx>>5)&1 ) {
35 | pr_info("VMX supported CPU\n");
36 | } else {
37 | pr_info("VMX not supported by CPU\n");
38 | }
39 | }
40 |
41 | // XOR
42 | void xor_f(void) {
43 | asm volatile (
44 | "xor %%rax, %%rax\n"
45 | :
46 | :
47 | : "%rax"
48 | );
49 | }
50 |
51 | // ** Begin Instructions That Cause VM Exits Unconditionally **
52 |
53 | // CPUID
54 | // Returns processor identification and feature information in the EAX, EBX, ECX, and EDX registers.
55 | void cpuid_f(void) {
56 | asm volatile (
57 | "cpuid\n"
58 | :
59 | :
60 | : "%rax", "%rbx", "%rcx", "%rdx"
61 | );
62 | }
63 |
64 | // GETSEC
65 | // The GETSEC instruction provides a capability leaf function for system software to discover
66 | // the available GETSEC leaf functions that are supported in a processor. Table 6-2 lists the
67 | // currently available GETSEC leaf functions.
68 | void getsec_f(void) {
69 | asm volatile (
70 | "getsec"
71 | :
72 | :
73 | : "%rax", "%rbx"
74 | );
75 | }
76 |
77 | // INVD
78 | // Invalidates (flushes) the processor's internal caches and issues a special-function bus
79 | // cycle that directs external caches to also flush themselves. Data held in internal caches
80 | // is not written back to main memory.
81 | void invd_f(void) {
82 | asm volatile (
83 | "invd"
84 | :
85 | :
86 | :);
87 | }
88 |
89 | // XSETBV
90 | // Writes the contents of registers EDX:EAX into the 64-bit extended control register (XCR)
91 | // specified in the ECX register. (On processors that support the Intel 64 architecture,
92 | // the high-order 32 bits of RCX are ignored.)
93 | void xsetbv_f(void) {
94 | int in_rax = 0;
95 | int out_rcx;
96 | int in_rdx = 0;
97 | asm volatile (
98 | "xsetbv"
99 | : "=c"(out_rcx)
100 | : "a"(in_rax), "d"(in_rdx)
101 | : "%rbx"
102 | );
103 | }
104 |
105 | /*
106 | // INVEPT
107 | // Invalidate Cached EPT Mappings
108 | void invept_f(void) {
109 | int operand = 0x0001;
110 | int type = 1;
111 | asm volatile(
112 | "invept %0, %1\n"
113 | :
114 | : "m"(operand), "r"(type)
115 | );
116 | }
117 | */
118 | // INVVPID
119 | // Invalidate Cached VPID Mappings
120 | //
121 |
122 | // VMCALL
123 | // Call to VM Monitor
124 | //
125 |
126 | // VMCLEAR
127 | // Clear Virtual-Machine Control Structure
128 | //
129 |
130 | // VMLAUNCH
131 | // Launch Virtual Machine
132 | //
133 |
134 | // VMPTRLD
135 | // Load Pointer to Virtual-Machine Control Structure
136 | //
137 |
138 | // VMPTRST
139 | // Store Pointer to Virtual-Machine Control Structure
140 | //
141 |
142 | // VMRESUME
143 | // Resume Virtual Machine
144 | //
145 |
146 | // VMXOFF
147 | // Leave VMX Operation
148 | //
149 |
150 | // VMXON
151 | // Enter VMX Operation
152 | //
153 |
154 | // ** End Instructions That Cause VM Exits Unconditionally **
155 |
156 | // ** Begin Instructions That Cause VM Exits Conditionally **
157 |
158 | // CLTS
159 | // The CLTS instruction causes a VM exit if the bits in position 3 (corresponding to CR0.TS)
160 | // are set in both the CR0 guest/host mask and the CR0 read shadow.
161 | // Description: Clears the task-switched (TS) flag in the CR0 register.
162 | void clts_f(void) {
163 | asm volatile (
164 | "clts"
165 | :
166 | :
167 | :
168 | );
169 | }
170 |
171 | // ENCLS
172 | // The ENCLS instruction causes a VM exit if the "enable ENCLS exiting" VM-execution control is 1 and one of the following is true:
173 | // - The value of EAX is less than 63 and the corresponding bit in the ENCLS-exiting bitmap is 1 (see Section 24.6.16).
174 | // - The value of EAX is greater than or equal to 63 and bit 63 in the ENCLS-exiting bitmap is 1.
175 | //
176 |
177 | // HLT
178 | // The HLT instruction causes a VM exit if the "HLT exiting" VM-execution control is 1.
179 | //
180 |
181 | // IN, INS/INSB/INSW/INSD
182 | // The behavior of each of these instructions is determined by the settings of the "unconditional I/O exiting" and "use I/O bitmaps" VM-execution controls:
183 | // - If both controls are 0, the instruction executes normally.
184 | // - If the "unconditional I/O exiting" VM-execution control is 1 and the "use I/O bitmaps" VM-execution control the instruction causes a VM exit.
185 | // - If the "use I/O bitmaps" VM-execution control is 1, the instruction causes a VM exit if it attempts to access an I/O port corresponding to a bit set to 1 in the appropriate I/O bitmap (see Section 24.6.4). If an I/O operation "wraps around" the 16-bit I/O-port space (accesses ports FFFFH and 0000H), the I/O instruction causes a VM exit (the "unconditional I/O exiting" VM-execution control is ignored if the "use I/O bitmaps" VM-execution control is 1).
186 | //See Section 25.1.1 for information regarding the priority of VM exits relative to faults that may be caused by the INS and OUTS instructions.
187 | void inb_f(void) {
188 | uint16_t port = 0x3F8; // Serial 1 Port
189 | uint8_t ret;
190 |
191 | asm volatile (
192 | "inb %1, %0"
193 | : "=a"(ret)
194 | : "Nd"(port)
195 | );
196 | }
197 | // OUT, OUTS/OUTSB/OUTSW/OUTSD
198 | // (See the exiting description in the "IN, INS/INSB/INSW/INSD" section above)
199 | void outb_f(void) {
200 | uint16_t port = 0x3F8; // Serial 1 Port
201 | uint8_t val = 0x0;
202 |
203 | asm volatile (
204 | "outb %0, %1"
205 | :
206 | : "a"(val), "Nd"(port)
207 | );
208 | }
209 |
210 | // INVLPG
211 | // The INVLPG instruction causes a VM exit if the "INVLPG exiting" VM-execution control is 1.
212 | // Description: Invalidates any translation lookaside buffer (TLB) entries specified with the
213 | // source operand. The source operand is a memory address. The processor determines the page
214 | // that contains that address and flushes all TLB entries for that page.
215 | void invlpg_f(void) {
216 | int m = 0x0;
217 |
218 | asm volatile (
219 | "invlpg (%0)"
220 | :
221 | : "b"(m)
222 | : "memory"
223 | );
224 | }
225 |
226 | // INVPCID
227 | // The INVPCID instruction causes a VM exit if the "INVLPG exiting" and "enable INVPCID"
228 | // VM-execution controls are both 1.
229 | void invpcid_f(void) {
230 | int desc = 0x0;
231 | int type = 0x0;
232 |
233 | asm volatile ( // Hex opcode is "invpcid (%ecx), %eax" in 32-bit mode and "invpcid (%rcx), %rax" in long mode.
234 | ".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
235 | :
236 | : "m"(desc), "a"(type), "c"(&desc)
237 | : "memory"
238 | );
239 | }
240 |
241 | // LGDT, LIDT, LLDT, LTR, SGDT, SIDT, SLDT, STR
242 | // These instructions cause VM exits if the "descriptor-table exiting" VM-execution control is 1.
243 | //
244 |
245 | // LMSW
246 | // In general, the LMSW instruction causes a VM exit if it would write, for any bit set in the low 4 bits of the CR0 guest/host mask, a value different than the corresponding bit in the CR0 read shadow. LMSW never clears bit 0 of CR0 (CR0.PE); thus, LMSW causes a VM exit if either of the following are true:
247 | // - The bits in position 0 (corresponding to CR0.PE) are set in both the CR0 guest/mask and the source operand, and the bit in position 0 is clear in the CR0 read shadow.
248 | // - For any bit position in the range 3:1, the bit in that position is set in the CR0 guest/mask and the values of the corresponding bits in the source operand and the CR0 read shadow differ.
249 | //
250 |
251 | // MONITOR
252 | // The MONITOR instruction causes a VM exit if the “MONITOR exiting” VM-execution control is 1.
253 | //
254 |
255 | // MOV from CR3
256 | // The MOV from CR3 instruction causes a VM exit if the “CR3-store exiting” VM-execution control is 1. The first processors to support the virtual-machine extensions supported only the 1-setting of this control.
257 | // Note: At the moment, this is effectively implemented in mov_from_cr3_f(), as it does a MOV from RAX, then a MOV to RAX [of the same value].
258 |
259 | // MOV from CR8
260 | // The MOV from CR8 instruction causes a VM exit if the “CR8-store exiting” VM-execution control is 1.
261 | void mov_from_cr8_f(void) {
262 | asm volatile("mov %cr8, %rax");
263 | }
264 |
265 | // MOV to CR0
266 | // The MOV to CR0 instruction causes a VM exit unless the value of its source operand matches, for the position of each bit set in the CR0 guest/host mask, the corresponding bit in the CR0 read shadow. (If every bit is clear in the CR0 guest/host mask, MOV to CR0 cannot cause a VM exit.)
267 | void mov_to_cr0_f(void) {
268 | asm volatile("mov %cr0, %rax\n\t \
269 | mov %rax, %cr0");
270 | }
271 |
272 | // MOV to CR3
273 | // The MOV to CR3 instruction causes a VM exit unless the “CR3-load exiting” VM-execution control is 0 or the value of its source operand is equal to one of the CR3-target values specified in the VMCS. Only the first n CR3-target values are considered, where n is the CR3-target count. If the “CR3-load exiting” VMexecution control is 1 and the CR3-target count is 0, MOV to CR3 always causes a VM exit.
274 | // The first processors to support the virtual-machine extensions supported only the 1-setting of the “CR3-load exiting” VM-execution control. These processors always consult the CR3-target controls to determine whether an execution of MOV to CR3 causes a VM exit.
275 | void mov_to_cr3_f(void) {
276 | asm volatile("mov %cr3, %rax\n\t \
277 | mov %rax, %cr3");
278 | }
279 |
280 | // MOV to CR4
281 | // The MOV to CR4 instruction causes a VM exit unless the value of its source operand matches, for the position of each bit set in the CR4 guest/host mask, the corresponding bit in the CR4 read shadow.
282 | void mov_to_cr4_f(void) {
283 | asm volatile("mov %cr4, %rax\n\t \
284 | mov %rax, %cr4");
285 | }
286 |
287 | // MOV to CR8
288 | // The MOV to CR8 instruction causes a VM exit if the “CR8-load exiting” VM-execution control is 1.
289 | void mov_to_cr8_f(void) {
290 | asm volatile("xor %rax, %rax\n\t \
291 | or $0x1, %rax\n\t \
292 | mov %rax, %cr8"); // Setting interrupt priority to the lowest (1).
293 | }
294 |
295 | // MOV DR
296 | // The MOV DR instruction causes a VM exit if the “MOV-DR exiting” VM-execution control is 1. Such VM exits represent an exception to the principles identified in Section 25.1.1 in that they take priority over the following: general-protection exceptions based on privilege level; and invalid-opcode exceptions that occur because CR4.DE=1 and the instruction specified access to DR4 or DR5.
297 | void mov_dr0_f(void) {
298 | asm volatile("mov %dr0, %rax\n\t \
299 | mov %rax, %dr0");
300 | }
301 | void mov_dr1_f(void) {
302 | asm volatile("mov %dr1, %rax\n\t \
303 | mov %rax, %dr1");
304 | }
305 | void mov_dr2_f(void) {
306 | asm volatile("mov %dr2, %rax\n\t \
307 | mov %rax, %dr2");
308 | }
309 | void mov_dr3_f(void) {
310 | asm volatile("mov %dr3, %rax\n\t \
311 | mov %rax, %dr3");
312 | }
313 | void mov_dr4_f(void) {
314 | asm volatile("mov %dr4, %rax\n\t \
315 | mov %rax, %dr4");
316 | }
317 | void mov_dr5_f(void) {
318 | asm volatile("mov %dr5, %rax\n\t \
319 | mov %rax, %dr5");
320 | }
321 | void mov_dr6_f(void) {
322 | asm volatile("mov %dr6, %rax\n\t \
323 | mov %rax, %dr6");
324 | }
325 | void mov_dr7_f(void) {
326 | asm volatile("mov %dr7, %rax\n\t \
327 | mov %rax, %dr7");
328 | }
329 |
330 | // MWAIT
331 | // The MWAIT instruction causes a VM exit if the “MWAIT exiting” VM-execution control is 1.
332 | // If this control is 0, the behavior of the MWAIT instruction may be modified (see Section 25.3).
333 | //
334 |
335 | // PAUSE
336 | // The behavior of each of this instruction depends on CPL and the settings of the “PAUSE exiting”
337 | // and “PAUSE-loop exiting” VM-execution controls:
338 | // - CPL = 0.
339 | // - If the “PAUSE exiting” and “PAUSE-loop exiting” VM-execution controls are both 0, the
340 | // instruction executes normally.
341 | // - If the “PAUSE exiting” VM-execution control is 1, the PAUSE instruction causes a VM
342 | // exit (the “PAUSEloop exiting” VM-execution control is ignored if CPL = 0 and the
343 | // “PAUSE exiting” VM-execution control is 1).
344 | // - If the “PAUSE exiting” VM-execution control is 0 and the “PAUSE-loop exiting”
345 | // VM-execution contro1, the following treatment applies.
346 | // The processor determines the amount of time between this execution of PAUSE and the
347 | // previous execution of PAUSE at CPL 0. If this amount of time exceeds the value of the
348 | // VM-execution control field PLE_Gap, the processor considers this execution to be the
349 | // first execution of PAUSE in a loop. (It also does so for the first execution of PAUSE
350 | // at CPL 0 after VM entry.)
351 | // Otherwise, the processor determines the amount of time since the most recent execution
352 | // of PAUSE that was considered to be the first in a loop. If this amount of time exceeds
353 | // the value of the VM-execution control field PLE_Window, a VM exit occurs.
354 | // For purposes of these computations, time is measured based on a counter that runs at
355 | // the same rate as the timestamp counter (TSC).
356 | // - CPL > 0.
357 | // - If the “PAUSE exiting” VM-execution control is 0, the PAUSE instruction executes normally.
358 | // - If the “PAUSE exiting” VM-execution control is 1, the PAUSE instruction causes a VM exit.
359 | // The “PAUSE-loop exiting” VM-execution control is ignored if CPL > 0.
360 | //
361 |
362 | // RDMSR
363 | // The RDMSR instruction causes a VM exit if any of the following are true:
364 | // - The “use MSR bitmaps” VM-execution control is 0.
365 | // - The value of ECX is not in the ranges 00000000H – 00001FFFH and C0000000H – C0001FFFH.
366 | // - The value of ECX is in the range 00000000H – 00001FFFH and bit n in read bitmap for low MSRs
367 | // is 1, where n is the value of ECX.
368 | // - The value of ECX is in the range C0000000H – C0001FFFH and bit n in read bitmap for high MSRs
369 | // is 1, where n is the value of ECX & 00001FFFH.
370 | // See Section 24.6.9 for details regarding how these bitmaps are identified.
371 | void rdmsr_f(void) {
372 | int out_rax;
373 | int in_rcx = 0x33;
374 | int out_rdx;
375 |
376 | asm volatile (
377 | "rdmsr"
378 | : "=a"(out_rax), "=d"(out_rdx)
379 | : "c"(in_rcx) // Reading TEST_CTL (see http://www.cs.inf.ethz.ch/stricker/lab/doc/intel-part4.pdf)
380 | : "%rbx"
381 | );
382 | }
383 |
384 | // RDPMC
385 | // The RDPMC instruction causes a VM exit if the “RDPMC exiting” VM-execution control is 1.
386 | void rdpmc_f(void) {
387 | unsigned a, d, c;
388 |
389 | c = (1<<30);
390 |
391 | asm volatile(
392 | "rdpmc"
393 | : "=a"(a), "=d"(d)
394 | : "c"(c)
395 | );
396 | }
397 |
398 | // RDRAND
399 | // The RDRAND instruction causes a VM exit if the “RDRAND exiting” VM-execution control is 1.
400 | void rdrand_f(void) {
401 | uint64_t data = 0;
402 |
403 | asm volatile (
404 | "rdrand %0"
405 | : "=rax"(data)
406 | );
407 | }
408 |
409 | // RDSEED
410 | // The RDSEED instruction causes a VM exit if the “RDSEED exiting” VM-execution control is 1.
411 | void rdseed_f(void) {
412 | uint64_t data = 0;
413 |
414 | asm volatile (
415 | "rdseed %0"
416 | : "=rax"(data)
417 | );
418 | }
419 |
420 | // RDTSC
421 | // The RDTSC instruction causes a VM exit if the “RDTSC exiting” VM-execution control is 1.
422 | void rdtsc_f(void) {
423 | asm volatile (
424 | "rdtsc"
425 | :
426 | :
427 | : "%rax", "%rdx"
428 | );
429 | }
430 |
431 | // RDTSCP
432 | // The RDTSCP instruction causes a VM exit if the “RDTSC exiting” and “enable RDTSCP”
433 | // VM-execution controls are both 1.
434 | void rdtscp_f(void) {
435 | asm volatile (
436 | "rdtscp"
437 | :
438 | :
439 | : "%rax", "%rcx", "%rdx"
440 | );
441 | }
442 |
443 | // RSM
444 | // The RSM instruction causes a VM exit if executed in system-management mode (SMM).
445 | //
446 |
447 | // VMREAD
448 | // Read Field from Virtual-Machine Control Structure
449 | // The VMREAD instruction causes a VM exit if any of the following are true:
450 | // - The “VMCS shadowing” VM-execution control is 0.
451 | // - Bits 63:15 (bits 31:15 outside 64-bit mode) of the register source operand are not all 0.
452 | // - Bit n in VMREAD bitmap is 1, where n is the value of bits 14:0 of the register source
453 | // operand. See Section24.6.15 for details regarding how the VMREAD bitmap is identified.
454 | // If the VMREAD instruction does not cause a VM exit, it reads from the VMCS referenced by the VMCS
455 | // link pointer. See Chapter 30, “VMREAD—Read Field from Virtual-Machine Control Structure” for
456 | // details of the operation of the VMREAD instruction.
457 | //
458 |
459 |
460 | // VMWRITE
461 | // Write Field to Virtual-Machine Control Structure
462 | // The VMWRITE instruction causes a VM exit if any of the following are true:
463 | // - The “VMCS shadowing” VM-execution control is 0.
464 | // - Bits 63:15 (bits 31:15 outside 64-bit mode) of the register source operand are not all 0.
465 | // - Bit n in VMWRITE bitmap is 1, where n is the value of bits 14:0 of the register source
466 | // operand. See Section 24.6.15 for details regarding how the VMWRITE bitmap is identified.
467 | // If the VMWRITE instruction does not cause a VM exit, it writes to the VMCS referenced by the
468 | // VMCS link pointer. See Chapter 30, “VMWRITE—Write Field to Virtual-Machine Control Structure”
469 | // for details of the operation of the VMWRITE instruction.
470 | //
471 |
472 | // The WBINVD instruction causes a VM exit if the “WBINVD exiting” VM-execution control is 1.
473 | void wbinvd_f(void) {
474 | asm volatile (
475 | "wbinvd"
476 | );
477 | }
478 |
479 | // WRMSR
480 | // The WRMSR instruction causes a VM exit if any of the following are true:
481 | // - The “use MSR bitmaps” VM-execution control is 0.
482 | // - The value of ECX is not in the ranges 00000000H – 00001FFFH and C0000000H – C0001FFFH.
483 | // - The value of ECX is in the range 00000000H – 00001FFFH and bit n in write bitmap for low MSRs is 1, where n is the value of ECX.
484 | // - The value of ECX is in the range C0000000H – C0001FFFH and bit n in write bitmap for high MSRs is 1, where n is the value of ECX & 00001FFFH.
485 | // See Section 24.6.9 for details regarding how these bitmaps are identified.
486 | // Description: Writes the contents of registers EDX:EAX into the 64-bit model specific register (MSR) specified in the ECX register.
487 | void wrmsr_f(void) {
488 | int in_rcx = 0x33;
489 | int in_eaxedx = 0x0000000000000000;
490 | asm volatile (
491 | "wrmsr"
492 | :
493 | : "c"(in_rcx), "A"(in_eaxedx)
494 | );
495 | }
496 |
497 | // XRSTORS
498 | // The XRSTORS instruction causes a VM exit if the “enable XSAVES/XRSTORS” VM-execution control is 1 and any bit is set in the logical-AND of the following three values: EDX:EAX, the IA32_XSS MSR, and the XSS-exiting bitmap (see Section 24.6.19).
499 | //
500 |
501 | // XSAVES
502 | // The XSAVES instruction causes a VM exit if the “enable XSAVES/XRSTORS” VM-execution control is 1 and any bit is set in the logical-AND of the following three values: EDX:EAX, the IA32_XSS MSR, and the XSSexiting bitmap (see Section 24.6.19).
503 | //
504 |
505 | // ** End Instructions That Cause VM Exits Conditionally **
506 |
507 | // ** Begin Instructions That Are Non-Temporal **
508 |
509 | // MOVNTDQA
510 | // Load Double Quadword Non-Temporal Aligned Hint
511 | void movntdqa_f(void) {
512 | asm volatile (
513 | "movntdqa (%0), %%xmm1\n\t"
514 | :
515 | : "c"(vmem_aligned)
516 | : "memory"
517 | );
518 | }
519 |
520 | // MOVNTDQ
521 | // Store Packed Integers Using Non-Temporal Hint
522 | void movntdq_f(void) {
523 | asm volatile (
524 | "movntdq %%xmm1, (%0)\n\t"
525 | :
526 | : "c"(vmem_aligned)
527 | : "memory"
528 | );
529 | }
530 |
531 | // ** End Instructions That Are Non-Temporal **
532 |
533 | // ** Begin Instructions That Cause a Write-Combining Buffer Flush **
534 |
535 | // CLFLUSH
536 | // Flush Cache Line
537 | void clflush_f(void) {
538 | asm volatile (
539 | "clflush 0(%0)"
540 | :
541 | : "c"(vmem_aligned)
542 | :
543 | );
544 | }
545 |
546 | // SFENCE
547 | // Store Fence
548 | void sfence_f(void) {
549 | asm volatile (
550 | "sfence"
551 | );
552 | }
553 |
554 | // MFENCE
555 | // Memory Fence
556 | void mfence_f(void) {
557 | asm volatile (
558 | "mfence"
559 | );
560 | }
561 |
562 | // ** End Instructions That Cause a Write-Combining Buffer Flush **
563 |
564 |
--------------------------------------------------------------------------------
/instructions.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #ifndef INSTRUCTIONS_H
21 | #define INSTRUCTIONS_H
22 |
23 | // Note: Not all instructions are necessarily implemented, but are still noted here for
24 | // completeness. This may be either because it is problematic to run on a guest, or
25 | // because it is simply not an instruction that is supported on a guest domain.
26 | // Many of the descriptions are obtained directly from the Intel manual.
27 |
28 | #include "main.h"
29 |
30 | #define SUSPEND_INTERRUPTS(FLAGS, CODE) \
31 | local_irq_save(FLAGS); \
32 | local_irq_disable(); \
33 | CODE \
34 | local_irq_restore(FLAGS); \
35 | local_irq_enable();
36 |
37 |
38 | extern uintptr_t *vmem_aligned;
39 |
40 | // CPU feature check for virtual machine extensions
41 | void cpuid_test_support_f(void);
42 |
43 | // XOR
44 | void xor_f(void);
45 |
46 | // ** Begin Instructions That Cause VM Exits Unconditionally **
47 |
48 | // CPUID
49 | // Returns processor identification and feature information in the EAX, EBX, ECX, and EDX registers.
50 | void cpuid_f(void);
51 |
52 | // GETSEC
53 | // The GETSEC instruction provides a capability leaf function for system software to discover
54 | // the available GETSEC leaf functions that are supported in a processor. Table 6-2 lists the
55 | // currently available GETSEC leaf functions.
56 | void getsec_f(void);
57 |
58 | // INVD
59 | // Invalidates (flushes) the processor's internal caches and issues a special-function bus
60 | // cycle that directs external caches to also flush themselves. Data held in internal caches
61 | // is not written back to main memory.
62 | void invd_f(void);
63 |
64 | // XSETBV
65 | // Writes the contents of registers EDX:EAX into the 64-bit extended control register (XCR)
66 | // specified in the ECX register. (On processors that support the Intel 64 architecture,
67 | // the high-order 32 bits of RCX are ignored.)
68 | void xsetbv_f(void);
69 |
70 | /*
71 | // INVEPT
72 | // Invalidate Cached EPT Mappings
73 | void invept_f(void) {
74 | int operand = 0x0001;
75 | int type = 1;
76 | asm volatile(
77 | "invept %0, %1\n"
78 | :
79 | : "m"(operand), "r"(type)
80 | );
81 | }
82 | */
83 | // INVVPID
84 | // Invalidate Cached VPID Mappings
85 | //
86 |
87 | // VMCALL
88 | // Call to VM Monitor
89 | //
90 |
91 | // VMCLEAR
92 | // Clear Virtual-Machine Control Structure
93 | //
94 |
95 | // VMLAUNCH
96 | // Launch Virtual Machine
97 | //
98 |
99 | // VMPTRLD
100 | // Load Pointer to Virtual-Machine Control Structure
101 | //
102 |
103 | // VMPTRST
104 | // Store Pointer to Virtual-Machine Control Structure
105 | //
106 |
107 | // VMRESUME
108 | // Resume Virtual Machine
109 | //
110 |
111 | // VMXOFF
112 | // Leave VMX Operation
113 | //
114 |
115 | // VMXON
116 | // Enter VMX Operation
117 | //
118 |
119 | // ** End Instructions That Cause VM Exits Unconditionally **
120 |
121 | // ** Begin Instructions That Cause VM Exits Conditionally **
122 |
123 | // CLTS
124 | // The CLTS instruction causes a VM exit if the bits in position 3 (corresponding to CR0.TS)
125 | // are set in both the CR0 guest/host mask and the CR0 read shadow.
126 | // Description: Clears the task-switched (TS) flag in the CR0 register.
127 | void clts_f(void);
128 |
129 | // ENCLS
130 | // The ENCLS instruction causes a VM exit if the "enable ENCLS exiting" VM-execution control is 1 and one of the following is true:
131 | // - The value of EAX is less than 63 and the corresponding bit in the ENCLS-exiting bitmap is 1 (see Section 24.6.16).
132 | // - The value of EAX is greater than or equal to 63 and bit 63 in the ENCLS-exiting bitmap is 1.
133 | //
134 |
135 | // HLT
136 | // The HLT instruction causes a VM exit if the "HLT exiting" VM-execution control is 1.
137 | //
138 |
139 | // IN, INS/INSB/INSW/INSD
140 | // The behavior of each of these instructions is determined by the settings of the "unconditional I/O exiting" and "use I/O bitmaps" VM-execution controls:
141 | // - If both controls are 0, the instruction executes normally.
142 | // - If the "unconditional I/O exiting" VM-execution control is 1 and the "use I/O bitmaps" VM-execution control the instruction causes a VM exit.
143 | // - If the "use I/O bitmaps" VM-execution control is 1, the instruction causes a VM exit if it attempts to access an I/O port corresponding to a bit set to 1 in the appropriate I/O bitmap (see Section 24.6.4). If an I/O operation "wraps around" the 16-bit I/O-port space (accesses ports FFFFH and 0000H), the I/O instruction causes a VM exit (the "unconditional I/O exiting" VM-execution control is ignored if the "use I/O bitmaps" VM-execution control is 1).
144 | //See Section 25.1.1 for information regarding the priority of VM exits relative to faults that may be caused by the INS and OUTS instructions.
145 | void inb_f(void);
146 |
147 | // OUT, OUTS/OUTSB/OUTSW/OUTSD
148 | // (See the exiting description in the "IN, INS/INSB/INSW/INSD" section above)
149 | void outb_f(void);
150 |
151 | // INVLPG
152 | // The INVLPG instruction causes a VM exit if the "INVLPG exiting" VM-execution control is 1.
153 | // Description: Invalidates any translation lookaside buffer (TLB) entries specified with the
154 | // source operand. The source operand is a memory address. The processor determines the page
155 | // that contains that address and flushes all TLB entries for that page.
156 | void invlpg_f(void);
157 |
158 | // INVPCID
159 | // The INVPCID instruction causes a VM exit if the "INVLPG exiting" and "enable INVPCID"
160 | // VM-execution controls are both 1.
161 | void invpcid_f(void);
162 |
163 | // LGDT, LIDT, LLDT, LTR, SGDT, SIDT, SLDT, STR
164 | // These instructions cause VM exits if the "descriptor-table exiting" VM-execution control is 1.
165 | //
166 |
167 | // LMSW
168 | // In general, the LMSW instruction causes a VM exit if it would write, for any bit set in the low 4 bits of the CR0 guest/host mask, a value different than the corresponding bit in the CR0 read shadow. LMSW never clears bit 0 of CR0 (CR0.PE); thus, LMSW causes a VM exit if either of the following are true:
169 | // - The bits in position 0 (corresponding to CR0.PE) are set in both the CR0 guest/mask and the source operand, and the bit in position 0 is clear in the CR0 read shadow.
170 | // - For any bit position in the range 3:1, the bit in that position is set in the CR0 guest/mask and the values of the corresponding bits in the source operand and the CR0 read shadow differ.
171 | //
172 |
173 | // MONITOR
174 | // The MONITOR instruction causes a VM exit if the “MONITOR exiting” VM-execution control is 1.
175 | //
176 |
177 | // MOV from CR3
178 | // The MOV from CR3 instruction causes a VM exit if the “CR3-store exiting” VM-execution control is 1. The first processors to support the virtual-machine extensions supported only the 1-setting of this control.
179 | // Note: At the moment, this is effectively implemented in mov_from_cr3_f(), as it does a MOV from RAX, then a MOV to RAX [of the same value].
180 |
181 | // MOV from CR8
182 | // The MOV from CR8 instruction causes a VM exit if the “CR8-store exiting” VM-execution control is 1.
183 | void mov_from_cr8_f(void);
184 |
185 | // MOV to CR0
186 | // The MOV to CR0 instruction causes a VM exit unless the value of its source operand matches, for the position of each bit set in the CR0 guest/host mask, the corresponding bit in the CR0 read shadow. (If every bit is clear in the CR0 guest/host mask, MOV to CR0 cannot cause a VM exit.)
187 | void mov_to_cr0_f(void);
188 |
189 | // MOV to CR3
190 | // The MOV to CR3 instruction causes a VM exit unless the “CR3-load exiting” VM-execution control is 0 or the value of its source operand is equal to one of the CR3-target values specified in the VMCS. Only the first n CR3-target values are considered, where n is the CR3-target count. If the “CR3-load exiting” VMexecution control is 1 and the CR3-target count is 0, MOV to CR3 always causes a VM exit.
191 | // The first processors to support the virtual-machine extensions supported only the 1-setting of the “CR3-load exiting” VM-execution control. These processors always consult the CR3-target controls to determine whether an execution of MOV to CR3 causes a VM exit.
192 | void mov_to_cr3_f(void);
193 |
194 | // MOV to CR4
195 | // The MOV to CR4 instruction causes a VM exit unless the value of its source operand matches, for the position of each bit set in the CR4 guest/host mask, the corresponding bit in the CR4 read shadow.
196 | void mov_to_cr4_f(void);
197 |
198 | // MOV to CR8
199 | // The MOV to CR8 instruction causes a VM exit if the “CR8-load exiting” VM-execution control is 1.
200 | void mov_to_cr8_f(void);
201 |
202 | // MOV DR
203 | // The MOV DR instruction causes a VM exit if the “MOV-DR exiting” VM-execution control is 1. Such VM exits represent an exception to the principles identified in Section 25.1.1 in that they take priority over the following: general-protection exceptions based on privilege level; and invalid-opcode exceptions that occur because CR4.DE=1 and the instruction specified access to DR4 or DR5.
204 | void mov_dr0_f(void);
205 | void mov_dr1_f(void);
206 | void mov_dr2_f(void);
207 | void mov_dr3_f(void);
208 | void mov_dr4_f(void);
209 | void mov_dr5_f(void);
210 | void mov_dr6_f(void);
211 | void mov_dr7_f(void);
212 |
213 | // MWAIT
214 | // The MWAIT instruction causes a VM exit if the “MWAIT exiting” VM-execution control is 1.
215 | // If this control is 0, the behavior of the MWAIT instruction may be modified (see Section 25.3).
216 | //
217 |
218 | // PAUSE
219 | // The behavior of each of this instruction depends on CPL and the settings of the “PAUSE exiting”
220 | // and “PAUSE-loop exiting” VM-execution controls:
221 | // - CPL = 0.
222 | // - If the “PAUSE exiting” and “PAUSE-loop exiting” VM-execution controls are both 0, the
223 | // instruction executes normally.
224 | // - If the “PAUSE exiting” VM-execution control is 1, the PAUSE instruction causes a VM
225 | // exit (the “PAUSEloop exiting” VM-execution control is ignored if CPL = 0 and the
226 | // “PAUSE exiting” VM-execution control is 1).
227 | // - If the “PAUSE exiting” VM-execution control is 0 and the “PAUSE-loop exiting”
228 | // VM-execution contro1, the following treatment applies.
229 | // The processor determines the amount of time between this execution of PAUSE and the
230 | // previous execution of PAUSE at CPL 0. If this amount of time exceeds the value of the
231 | // VM-execution control field PLE_Gap, the processor considers this execution to be the
232 | // first execution of PAUSE in a loop. (It also does so for the first execution of PAUSE
233 | // at CPL 0 after VM entry.)
234 | // Otherwise, the processor determines the amount of time since the most recent execution
235 | // of PAUSE that was considered to be the first in a loop. If this amount of time exceeds
236 | // the value of the VM-execution control field PLE_Window, a VM exit occurs.
237 | // For purposes of these computations, time is measured based on a counter that runs at
238 | // the same rate as the timestamp counter (TSC).
239 | // - CPL > 0.
240 | // - If the “PAUSE exiting” VM-execution control is 0, the PAUSE instruction executes normally.
241 | // - If the “PAUSE exiting” VM-execution control is 1, the PAUSE instruction causes a VM exit.
242 | // The “PAUSE-loop exiting” VM-execution control is ignored if CPL > 0.
243 | //
244 |
245 | // RDMSR
246 | // The RDMSR instruction causes a VM exit if any of the following are true:
247 | // - The “use MSR bitmaps” VM-execution control is 0.
248 | // - The value of ECX is not in the ranges 00000000H – 00001FFFH and C0000000H – C0001FFFH.
249 | // - The value of ECX is in the range 00000000H – 00001FFFH and bit n in read bitmap for low MSRs
250 | // is 1, where n is the value of ECX.
251 | // - The value of ECX is in the range C0000000H – C0001FFFH and bit n in read bitmap for high MSRs
252 | // is 1, where n is the value of ECX & 00001FFFH.
253 | // See Section 24.6.9 for details regarding how these bitmaps are identified.
254 | void rdmsr_f(void);
255 |
256 | // RDPMC
257 | // The RDPMC instruction causes a VM exit if the “RDPMC exiting” VM-execution control is 1.
258 | void rdpmc_f(void);
259 |
260 | // RDRAND
261 | // The RDRAND instruction causes a VM exit if the “RDRAND exiting” VM-execution control is 1.
262 | void rdrand_f(void);
263 |
264 | // RDSEED
265 | // The RDSEED instruction causes a VM exit if the “RDSEED exiting” VM-execution control is 1.
266 | void rdseed_f(void);
267 |
268 | // RDTSC
269 | // The RDTSC instruction causes a VM exit if the “RDTSC exiting” VM-execution control is 1.
270 | void rdtsc_f(void);
271 |
272 | // RDTSCP
273 | // The RDTSCP instruction causes a VM exit if the “RDTSC exiting” and “enable RDTSCP”
274 | // VM-execution controls are both 1.
275 | void rdtscp_f(void);
276 |
277 | // RSM
278 | // The RSM instruction causes a VM exit if executed in system-management mode (SMM).
279 | //
280 |
281 | // VMREAD
282 | // Read Field from Virtual-Machine Control Structure
283 | // The VMREAD instruction causes a VM exit if any of the following are true:
284 | // - The “VMCS shadowing” VM-execution control is 0.
285 | // - Bits 63:15 (bits 31:15 outside 64-bit mode) of the register source operand are not all 0.
286 | // - Bit n in VMREAD bitmap is 1, where n is the value of bits 14:0 of the register source
287 | // operand. See Section24.6.15 for details regarding how the VMREAD bitmap is identified.
288 | // If the VMREAD instruction does not cause a VM exit, it reads from the VMCS referenced by the VMCS
289 | // link pointer. See Chapter 30, “VMREAD—Read Field from Virtual-Machine Control Structure” for
290 | // details of the operation of the VMREAD instruction.
291 | //
292 |
293 |
294 | // VMWRITE
295 | // Write Field to Virtual-Machine Control Structure
296 | // The VMWRITE instruction causes a VM exit if any of the following are true:
297 | // - The “VMCS shadowing” VM-execution control is 0.
298 | // - Bits 63:15 (bits 31:15 outside 64-bit mode) of the register source operand are not all 0.
299 | // - Bit n in VMWRITE bitmap is 1, where n is the value of bits 14:0 of the register source
300 | // operand. See Section 24.6.15 for details regarding how the VMWRITE bitmap is identified.
301 | // If the VMWRITE instruction does not cause a VM exit, it writes to the VMCS referenced by the
302 | // VMCS link pointer. See Chapter 30, “VMWRITE—Write Field to Virtual-Machine Control Structure”
303 | // for details of the operation of the VMWRITE instruction.
304 | //
305 |
306 | // The WBINVD instruction causes a VM exit if the “WBINVD exiting” VM-execution control is 1.
307 | void wbinvd_f(void);
308 |
309 | // WRMSR
310 | // The WRMSR instruction causes a VM exit if any of the following are true:
311 | // - The “use MSR bitmaps” VM-execution control is 0.
312 | // - The value of ECX is not in the ranges 00000000H – 00001FFFH and C0000000H – C0001FFFH.
313 | // - The value of ECX is in the range 00000000H – 00001FFFH and bit n in write bitmap for low MSRs is 1, where n is the value of ECX.
314 | // - The value of ECX is in the range C0000000H – C0001FFFH and bit n in write bitmap for high MSRs is 1, where n is the value of ECX & 00001FFFH.
315 | // See Section 24.6.9 for details regarding how these bitmaps are identified.
316 | // Description: Writes the contents of registers EDX:EAX into the 64-bit model specific register (MSR) specified in the ECX register.
317 | void wrmsr_f(void);
318 |
319 | // XRSTORS
320 | // The XRSTORS instruction causes a VM exit if the “enable XSAVES/XRSTORS” VM-execution control is 1 and any bit is set in the logical-AND of the following three values: EDX:EAX, the IA32_XSS MSR, and the XSS-exiting bitmap (see Section 24.6.19).
321 | //
322 |
323 | // XSAVES
324 | // The XSAVES instruction causes a VM exit if the “enable XSAVES/XRSTORS” VM-execution control is 1 and any bit is set in the logical-AND of the following three values: EDX:EAX, the IA32_XSS MSR, and the XSSexiting bitmap (see Section 24.6.19).
325 | //
326 |
327 | // ** End Instructions That Cause VM Exits Conditionally **
328 |
329 | // ** Begin Instructions That Are Non-Temporal **
330 |
331 | // MOVNTDQA
332 | // Load Double Quadword Non-Temporal Aligned Hint
333 | void movntdqa_f(void);
334 |
335 | // MOVNTDQ
336 | // Store Packed Integers Using Non-Temporal Hint
337 | void movntdq_f(void);
338 |
339 | // ** End Instructions That Are Non-Temporal **
340 |
341 | // ** Begin Instructions That Cause a Write-Combining Buffer Flush **
342 |
343 | // CLFLUSH
344 | // Flush Cache Line
345 | void clflush_f(void);
346 |
347 | // SFENCE
348 | // Store Fence
349 | void sfence_f(void);
350 |
351 | // MFENCE
352 | // Memory Fence
353 | void mfence_f(void);
354 |
355 | // ** End Instructions That Cause a Write-Combining Buffer Flush **
356 |
357 | #endif
358 |
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 |
30 | #include "main.h"
31 |
32 | // These variables are used to kmalloc() memory for non-temporal detection and cache/memory timing
33 | uintptr_t *vmem;
34 | uintptr_t *vmem_aligned;
35 |
36 | static int timing_open(struct inode *inode, struct file *file) {
37 | (void) inode;
38 | (void) file;
39 |
40 | pr_alert("IOCTL open succeeded\n");
41 | return 0;
42 | }
43 |
44 | static int timing_release(struct inode *inode, struct file *file) {
45 | (void) inode;
46 | (void) file;
47 |
48 | pr_alert("IOCTL release succeeded\n");
49 | return 0;
50 | }
51 |
52 | static long timing_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
53 |
54 | (void) file;
55 |
56 | pr_info("Command received (cmd): 0x%x\n", cmd);
57 |
58 | switch ( cmd ) {
59 | case TIMING_IOCTL_NOP:
60 | pr_info("Finished without doing anything (NOP requested)\n");
61 | break;
62 | case TIMING_IOCTL_WALL:
63 | pr_info("Commencing wall timing measurements...\n");
64 | return (wall_timing_start());
65 | case TIMING_IOCTL_THREAD:
66 | pr_info("Commencing NOP timing measurements...\n");
67 | return (nop_timing_start(1));
68 | case TIMING_IOCTL_CACHE:
69 | pr_info("Commencing cache timing measurements...\n");
70 | if ( !vmem ) {
71 | pr_alert("Kernel memory not allocated!");
72 | return -EFAULT;
73 | }
74 | return (cache_timing_start());
75 | case TIMING_IOCTL_MEMORY:
76 | pr_info("Commencing memory timing measurements...\n");
77 | if ( !vmem ) {
78 | pr_alert("Kernel memory not allocated!");
79 | return -EFAULT;
80 | }
81 | return (memory_timing_start());
82 | case TIMING_IOCTL_KMALLOC:
83 | pr_info("Allocating memory (kmalloc())\n");
84 | if ( !vmem ) {
85 | vmem = kmalloc(KMALLOC_SIZE, GFP_KERNEL);
86 | vmem_aligned = (uintptr_t *) ( ((uintptr_t) vmem + 15) & ~(uintptr_t) 0x0F ); // Align for load ops
87 | }
88 | if ( !*vmem || !*vmem_aligned ) {
89 | pr_alert("Kernel memory allocation failed!");
90 | return -EFAULT;
91 | }
92 | pr_info("Memory allocated at virtual address 0x%p (aligned at 0x%p) with a buffer size of %d bytes\n", \
93 | (void *) *vmem, (void *) *vmem_aligned, (int) KMALLOC_SIZE);
94 | break;
95 | case TIMING_IOCTL_WALL_FL:
96 | pr_info("Commencing buffer flush wall timing measurements...\n");
97 | return (buffer_flush_start());
98 | case TIMING_IOCTL_THREAD_FL:
99 | pr_info("Commencing buffer flush NOP timing measurements...\n");
100 | return (nop_timing_start(0));
101 | default:
102 | return -EINVAL;
103 | }
104 |
105 | return 0;
106 | }
107 |
108 | static struct file_operations fops = {
109 | .open = timing_open,
110 | .release = timing_release,
111 | .unlocked_ioctl = timing_unlocked_ioctl,
112 | };
113 |
114 | static struct miscdevice ecr_dev = {
115 | MISC_DYNAMIC_MINOR,
116 | "ECR",
117 | &fops
118 | };
119 |
120 | int timing_init_module(void) {
121 | pr_info("Inserting ECR kernel object\n");
122 |
123 | if ( misc_register(&ecr_dev) != 0 )
124 | {
125 | pr_alert("misc_register failed\n");
126 | return -EPERM;
127 | }
128 |
129 | return 0;
130 | }
131 |
132 | void timing_cleanup_module(void) {
133 | kfree(vmem);
134 | misc_deregister(&ecr_dev);
135 |
136 | pr_alert("Removing ECR kernel object\n");
137 | }
138 |
139 | module_init(timing_init_module);
140 | module_exit(timing_cleanup_module);
141 |
142 | MODULE_LICENSE("GPL");
143 | MODULE_DESCRIPTION("Environmental Characterization and Reponse");
144 | MODULE_AUTHOR("Assured Information Security, Inc");
145 |
--------------------------------------------------------------------------------
/main.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #ifndef MAIN_H
21 | #define MAIN_H
22 |
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include
33 |
34 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)
35 | #include
36 | #endif
37 |
38 | #include "mod_ioctl.h"
39 | #include "instructions.h"
40 | #include "wall_timer.h"
41 | #include "nop_timer.h"
42 | #include "cache_timer.h"
43 | #include "memory_timer.h"
44 |
45 | #define KMALLOC_SIZE sizeof(uint64_t) * 64
46 |
47 | extern int wall_timing_start(void);
48 | extern int nop_timing_start(bool action);
49 | extern int cache_timing_start(void);
50 | extern int memory_timing_start(void);
51 |
52 | #endif
53 |
--------------------------------------------------------------------------------
/memory_timer.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include "main.h"
21 | #include "memory_timer.h"
22 |
23 | struct page_struct {
24 | char bytes[PAGE_SIZE];
25 | };
26 |
27 | unsigned long memory_timing_probe(char *addr) {
28 | volatile unsigned long time;
29 |
30 | asm volatile(
31 | "mfence\n"
32 | "lfence\n"
33 | "rdtsc\n"
34 | "lfence\n"
35 | "movl %%eax, %%esi\n"
36 | "movl (%1), %%eax\n"
37 | "lfence\n"
38 | "rdtsc\n"
39 | "subl %%esi, %%eax\n"
40 | : "=a"(time)
41 | : "c"(addr)
42 | : "%esi", "%edx"
43 | );
44 | return time;
45 | }
46 |
47 | int memory_timing_start(void) {
48 | unsigned long t;
49 | int i;
50 | extern uintptr_t *vmem_aligned;
51 |
52 | void *ptv = vmem_aligned;
53 |
54 | struct page_struct *array = ptv;
55 |
56 | for ( i = 0; i < 10; i++ ) {
57 | t = memory_timing_probe((char *) &array[i]);
58 | t = memory_timing_probe((char *) &array[i]);
59 | pr_info("Cycles to access page %d: %d\n", i, (int)t);
60 | }
61 |
62 | return 0;
63 | }
64 |
--------------------------------------------------------------------------------
/memory_timer.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #ifndef MEMORY_TIMER_H
21 | #define MEMORY_TIMER_H
22 |
23 | unsigned long probe(char *addr);
24 | int memory_timing_start(void);
25 |
26 | #endif
27 |
--------------------------------------------------------------------------------
/mod_ioctl.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Environmental Characterization & Response
3 | * Copyright (C) 2018 Assured Information Security, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include "mod_ioctl.h"
30 |
31 | long strtoul_handler(char *val, int base) {
32 | long in_arg;
33 | char *endptr;
34 |
35 | in_arg = strtoul(val, &endptr, base);
36 |
37 | // Check for various possible errors
38 | if ( (errno == ERANGE && (in_arg == LONG_MAX || in_arg == LONG_MIN)) || (errno != 0 && in_arg == 0) ) {
39 | perror("Error encountered in strtoul_handler.");
40 | exit(EXIT_FAILURE);
41 | }
42 | if ( endptr == val ) {
43 | perror("No digits were found\n");
44 | exit(EXIT_FAILURE);
45 | }
46 |
47 | // If this is reached, strtroul() successfully parsed a number
48 |
49 | if ( *endptr != '\0' ) // Not necessarily an error
50 | printf("Further characters after number: %s\n", endptr);
51 |
52 | return in_arg;
53 | }
54 |
55 | int main(int argc, char **argv) {
56 | int fd, ret;
57 | timing_ioctl_struct arg_struct;
58 |
59 | if ( argc < 2 ) {
60 | puts("Usage: ./prog