Skip to contentSkip to conversation

CODING_STYLE: Document how to handle unexpected conditions

PATCH Superseded George Dunlap 3 participants 6 messages Feb 28, 2019 – Mar 4, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CODING_STYLE: Document how to handle unexpected conditions

It's not always clear what the best way is to handle unexpected
conditions: whether with ASSERT(), BUG_ON(), or some other method. All methods have a risk of introducing security vulnerabilities and unnecessary instabilities to production systems.

Document when to try to return an error for unexpected conditions, when to use BUG_ON(), and when to use ASSERT().

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Konrad Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

---
 CODING_STYLE | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
CODING_STYLE +79 -0
index 6cc5b774cf..51159970c0 100644
@@ -120,3 +120,82 @@ the end of files.  It should be:
  * indent-tabs-mode: nil
  * End:
  */
+
+Handling unexpected conditions
+------------------------------
+
+GUIDELINES:
+
+Passing errors up the stack should be used when the caller is already
+expecting to handle errors, and the state when the error was
+discovered isn’t broken, or too hard to fix.
+
+BUG_ON() should be used when you can’t pass errors up the stack, and
+continuing would certainly cause a vulnerability.
+
+ASSERT() should be used when continuing might work, or might have an
+effect later whose badness is equal or less than that of a host crash;
+OR whose truth can be clearly observed from the code directly
+surrounding it.  In particular, using an ASSERT() along with returning
+an error code, when suitable, is a helpful pattern for finding
+violations of assumptions during testing, but minimizing impact on
+production hypervisors.
+
+RATIONALE:
+
+It's frequently the case that code is writen with the assumption that
+certain conditions can never happen.  There are several possible
+actions programmers can take in these situations:
+
+* Programmers can simply not handle those cases in any way, other than
+perhaps to write a comment documenting what the assumption is.
+
+* Programmers can try to handle the case gracefully -- fixing up
+in-progress state and returning an error to the user.
+
+* Programmers can use ASSERT(), which will cause the check to be
+executed in DEBUG builds, and cause the hypervisor to crash if it's
+violated
+
+* Programmers can use BUG_ON(), which will cause the check to be
+executed in both DEBUG and non-DEBUG builds, and cause the hypervisor
+to crash if it's violated.
+
+In selecting which response to use, we want to achieve several goals:
+
+- To minimize risk of introducing security vulnerabilities,
+  particularly as the code evolves over time
+
+- To efficiently spend programmer time
+
+- To detect violations of assumptions as early as possible
+
+- To minimize the impact of bugs on production use cases
+
+The guidelines above attempt to balance these:
+
+- When the caller is expecting to handle errors, and there are no
+broken state at the time the unexpected condition is discovered, or
+when fixing the state is straightforward, then fixing up the state and
+returning an error is the most robust thing to do.  However, if the
+caller isn't expecting to handle errors, or if the state is difficult
+to fix, then returning an error may require extensive refactoring,
+which is not a good use of programmer time when they're certain that
+this condition cannot occur.
+
+- BUG_ON() will stop all hypervisor action immediately.  In situations
+where continuing might allow an attacker to escalate privilege, a
+BUG_ON() can change a privilege escalation or information leak into a
+denial-of-service (an improvement).  But in situations where
+continuing (say, returning an error) might be safe, then BUG_ON() can
+change a benign failure into denial-of-service (a degradation)
+
+- ASSERT() will stop the hypervisor during development, but allow
+hypervisor action to continue during production.  In situations where
+continuing will at worst result in a denial-of-service, and at best
+may have little effect other than perhaps quirky behavior, using an
+ASSERT() will allow violation of assumptions to be detected as soon as
+possible, while not causing undue degradation in production
+hypervisors.  However, in situations where continuing could cause
+privilege escalation or information leaks, using an ASSERT() can
+introduce security vulnerabilities.
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line

George Dunlap <[email protected]> 02/28/19 7:50 PM >>>

CODING_STYLE +6 -0
+GUIDELINES:
+
+Passing errors up the stack should be used when the caller is already
+expecting to handle errors, and the state when the error was
+discovered isn’t broken, or too hard to fix.
+

CODING_STYLE the end of files. It should be: · line 142

Quoted 10 lines
CODING_STYLE +10 -0
+BUG_ON() should be used when you can’t pass errors up the stack, and
+continuing would certainly cause a vulnerability.
+
+ASSERT() should be used when continuing might work, or might have an
+effect later whose badness is equal or less than that of a host crash;
+OR whose truth can be clearly observed from the code directly
+surrounding it.  In particular, using an ASSERT() along with returning
+an error code, when suitable, is a helpful pattern for finding
+violations of assumptions during testing, but minimizing impact on
+production hypervisors.

Along with these I strongly think the option of domain_crash() ought to be listed, and it should always be preferred over BUG_ON() as long as context permits.

Quoted 5 lines
CODING_STYLE +5 -0
+RATIONALE:
+
+It's frequently the case that code is writen with the assumption that
+certain conditions can never happen.  There are several possible
+actions programmers can take in these situations:

CODING_STYLE the end of files. It should be: · line 158

Quoted 10 lines
CODING_STYLE +10 -0
+
+* Programmers can simply not handle those cases in any way, other than
+perhaps to write a comment documenting what the assumption is.
+
+* Programmers can try to handle the case gracefully -- fixing up
+in-progress state and returning an error to the user.
+
+* Programmers can use ASSERT(), which will cause the check to be
+executed in DEBUG builds, and cause the hypervisor to crash if it's
+violated

Is it perhaps worth calling out explicitly that the supposed crash may occur much later, in a different context, and hence be perhaps rather difficult to analyze/debug?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line

George Dunlap <[email protected]> 02/28/19 7:50 PM >>>

CODING_STYLE +12 -0
+GUIDELINES:
+
+Passing errors up the stack should be used when the caller is already
+expecting to handle errors, and the state when the error was
+discovered isn’t broken, or too hard to fix.
+
+BUG_ON() should be used when you can’t pass errors up the stack, and
+continuing would certainly cause a vulnerability.
+
+ASSERT() should be used when continuing might work, or might have an
+effect later whose badness is equal or less than that of a host crash;
+OR whose truth can be clearly observed from the code directly
Quoted 8 lines

CODING_STYLE the end of files. It should be: · line 142

Quoted 4 lines
CODING_STYLE +4 -0
+surrounding it.  In particular, using an ASSERT() along with returning
+an error code, when suitable, is a helpful pattern for finding
+violations of assumptions during testing, but minimizing impact on
+production hypervisors.

Along with these I strongly think the option of domain_crash() ought to be listed, and it should always be preferred over BUG_ON() as long as context permits.

Ah, right, I totally forgot about this case. I'll revise and add this in.

Quoted 11 lines
Quoted 11 lines
CODING_STYLE +11 -0
+RATIONALE:
+
+It's frequently the case that code is writen with the assumption that
+certain conditions can never happen.  There are several possible
+actions programmers can take in these situations:
+
+* Programmers can simply not handle those cases in any way, other than
+perhaps to write a comment documenting what the assumption is.
+
+* Programmers can try to handle the case gracefully -- fixing up
+in-progress state and returning an error to the user.
Quoted 8 lines

CODING_STYLE the end of files. It should be: · line 158

Quoted 4 lines
CODING_STYLE +4 -0
+
+* Programmers can use ASSERT(), which will cause the check to be
+executed in DEBUG builds, and cause the hypervisor to crash if it's
+violated

Is it perhaps worth calling out explicitly that the supposed crash may occur much later, in a different context, and hence be perhaps rather difficult to analyze/debug?

Sorry, I don't quite understand this -- when you trigger an ASSERT() it crashes right away last time I checked. Did you mean instead to reply to the ASSERT() section of the GUIDELINES, which says you can use ASSERT if it may have an effect later whose badness is equal to or less than a host crash? Or did you mean I should call out that ASSERT()s can be used to catch errors closer to the source, rather than later?

It also occurs to me that ASSERT()s are really orthogonal to the other
three: At each point, you should consider whether in a production hypervisor you should 1) do nothing, 2) return an error, 3) crash the domain, or 4) crash the hypervisor; and in the case of 1-3, you might also want to add an ASSERT to move the detection of unexpected state closer to the point where it happens.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

George Dunlap <[email protected]> 03/01/19 6:39 PM >>>

Quoted 8 lines · Jan Beulich

CODING_STYLE the end of files. It should be: · line 158

Quoted 4 lines
Quoted 1 line
Quoted 1 line

George Dunlap <[email protected]> 02/28/19 7:50 PM >>>

CODING_STYLE +3 -0
+* Programmers can use ASSERT(), which will cause the check to be
+executed in DEBUG builds, and cause the hypervisor to crash if it's
+violated

Is it perhaps worth calling out explicitly that the supposed crash may occur much later, in a different context, and hence be perhaps rather difficult to analyze/debug?

Quoted 5 lines

Sorry, I don't quite understand this -- when you trigger an ASSERT() it crashes right away last time I checked. Did you mean instead to reply to the ASSERT() section of the GUIDELINES, which says you can use ASSERT if it may have an effect later whose badness is equal to or less than a host crash?

I did indeed pick the wrong context for my reply, and had meant that other place instead.

Quoted 6 lines

It also occurs to me that ASSERT()s are really orthogonal to the other
three: At each point, you should consider whether in a production hypervisor you should 1) do nothing, 2) return an error, 3) crash the domain, or 4) crash the hypervisor; and in the case of 1-3, you might also want to add an ASSERT to move the detection of unexpected state closer to the point where it happens.

Agreed.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to George Dunlap
Quoted 29 lines · George Dunlap

It's not always clear what the best way is to handle unexpected
conditions: whether with ASSERT(), BUG_ON(), or some other method. All methods have a risk of introducing security vulnerabilities and unnecessary instabilities to production systems.

Document when to try to return an error for unexpected conditions, when to use BUG_ON(), and when to use ASSERT().

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Konrad Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

---
 CODING_STYLE | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
CODING_STYLE +0 -0
index 6cc5b774cf..51159970c0 100644
@@ -120,3 +120,82 @@ the end of files.  It should be:
  * indent-tabs-mode: nil
  * End:

CODING_STYLE the end of files. It should be: · line 131

Quoted 10 lines
CODING_STYLE +9 -0
  */
+
+Handling unexpected conditions
+------------------------------
+
+GUIDELINES:
+
+Passing errors up the stack should be used when the caller is already
+expecting to handle errors, and the state when the error was
+discovered isn’t broken, or too hard to fix.

If the error is too hard to fix, what should the caller do after getting an error? Should Xen continue with such broken state?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 35 lines · Wei Liu
Quoted 34 lines · George Dunlap

It's not always clear what the best way is to handle unexpected
conditions: whether with ASSERT(), BUG_ON(), or some other method. All methods have a risk of introducing security vulnerabilities and unnecessary instabilities to production systems.

Document when to try to return an error for unexpected conditions, when to use BUG_ON(), and when to use ASSERT().

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Konrad Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

---
 CODING_STYLE | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
CODING_STYLE +4 -0
index 6cc5b774cf..51159970c0 100644
@@ -120,3 +120,82 @@ the end of files.  It should be:
  * indent-tabs-mode: nil
  * End:
  */
+
+Handling unexpected conditions
+------------------------------
+
Quoted 8 lines

CODING_STYLE the end of files. It should be: · line 131

Quoted 5 lines
CODING_STYLE +5 -0
+GUIDELINES:
+
+Passing errors up the stack should be used when the caller is already
+expecting to handle errors, and the state when the error was
+discovered isn’t broken, or too hard to fix.

If the error is too hard to fix, what should the caller do after getting an error? Should Xen continue with such broken state?

Right, I was sort of afraid this would be ambugious. This was meant to be "isn't broken or isn't too hard to fix". If it isn't broken, you can return, even if you're pretty sure the condition never happens. If it's broken but it's easy to fix, then fix it and return, even if you're pretty sure the condition never happens. If it's broken and not easy to fix, it may not be worth developer time handling that case, since you're pretty sure it never happens.

I'll try to make this clearer.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel