Closed Bug 1398874 Opened 9 years ago Closed 8 years ago

Add a static analysis to reject ParamTraits specializations with custom enum serialization

Categories

(Developer Infrastructure :: Source Code Analysis, enhancement, P3)

enhancement

Tracking

(firefox59 fixed)

RESOLVED FIXED
mozilla59
Tracking Status
firefox59 --- fixed

People

(Reporter: tedd, Assigned: Alex_Gaynor)

References

Details

(Keywords: sec-audit, Whiteboard: [post-critsmash-triage][adv-main59-])

Attachments

(1 file, 3 obsolete files)

While looking at ParamTraits<> specializations as part of the IPC message serialization part, I have seen in the past that the existing helper specializations [1] for serializing enums etc, are not used consistently. In one case the custom implementation simply static_cast'ed the received int to the enum type, however through that cast the bounds of the enum are not enforced. Which lead to a bug that would have allowed a compromised child to trigger an out-of-bounds in the parent (Bug 1303713). I am working on a static analysis that tries to detect these cases and throw an error during the build, also suggesting to use the helper specializations [1]. This bug should also be used to track existing issues before we can land the analysis. This is the first time that I am writing a custom analysis so any feedback is greatly appreciated (especially when I am doing things far too complicated) Also, I am marking this bug as a security bug since these issues can potentially lead to vulnerabilities. [1] http://searchfox.org/mozilla-central/rev/00fa5dacedb925022f53d025121f1a919508e7ce/ipc/glue/IPCMessageUtils.h#212-215
Attachment #8906704 - Flags: feedback?
Attachment #8906704 - Flags: feedback? → feedback?(ehsan)
Doing a build on my Linux machine, two places show up that do not use the serialization helper, one mozilla::EventMessage in widget/nsGUIEventIPC.h and the other the serialization of the NPN* types in dom/plugins/ipc/PluginMessageUtils.h. The patches for both locations should be fairly straight forward, but I am waiting on the feedback before filing dependent bugs.
Group: core-security → dom-core-security
Keywords: sec-audit
Comment on attachment 8906704 [details] [diff] [review] WIP - Reject custom ParamTraits<> specializations for enums Review of attachment 8906704 [details] [diff] [review]: ----------------------------------------------------------------- Looks like you're on the right path :-) - Just have a few (mostly style) nits. ::: build/clang-plugin/Checks.inc @@ +25,4 @@ > CHECK(NonParamInsideFunctionDeclChecker, "non-memmovable-template-arg") > CHECK(OverrideBaseCallChecker, "override-base-call") > CHECK(OverrideBaseCallUsageChecker, "override-base-call-usage") > +CHECK(ParamTraitsSpecializationHelperChecker, "paramtraits-specialization-helper") nit: This checker is only checking enum speciailzations AFAICT - perhaps ParamTraitsEnumChecker would be a better name? ::: build/clang-plugin/ParamTraitsSpecializationHelperChecker.cpp @@ +6,5 @@ > +#include "CustomMatchers.h" > + > +void ParamTraitsSpecializationHelperChecker::registerMatchers(MatchFinder* AstMatcher) { > + AstMatcher->addMatcher( > + classTemplateSpecializationDecl(isParamTraitsSpecialization()).bind("decl"), this); Please use hasName("ParmaTraits") here rather than the custom AST matcher. I _think_ it should work :-). @@ +11,5 @@ > +} > + > +void ParamTraitsSpecializationHelperChecker::check( > + const MatchFinder::MatchResult &Result) { > + const ClassTemplateSpecializationDecl *decl = nit: Variables in this file are in UpperCamelCase as we follow LLVM style @@ +15,5 @@ > + const ClassTemplateSpecializationDecl *decl = > + Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("decl"); > + > + for (DeclContext::decl_iterator iter = decl->decls_begin(); > + iter != decl->decls_end(); iter++) { nit: Please use a ranged-for loop over `decl->decls()`, e.g. for (auto& Inner : Decl->decls()) { ... } @@ +16,5 @@ > + Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("decl"); > + > + for (DeclContext::decl_iterator iter = decl->decls_begin(); > + iter != decl->decls_end(); iter++) { > + switch (iter->getKind()) { nit: Rather than using this switch please just try to dyn_cast once. if (auto* Def = dyn_cast<TypedefDecl>(Inner)) { ... } @@ +22,5 @@ > + TypedefDecl *def = dyn_cast<TypedefDecl>(*iter); > + QualType t = def->getUnderlyingType(); > + QualType c = t.getCanonicalType(); > + > + const clang::Type *ptr = c.getTypePtrOrNull(); nit: I think that this file should already be `using namespace clang` so clang:: isn't necessary. @@ +29,5 @@ > + } > + > + if (ptr->isEnumeralType()) { > + diag(decl->getLocStart(), "Custom ParamTraits implementation for an enum type, please consider using ContiguousEnumSerializer etc.", > + DiagnosticIDs::Error); Please split the suggestion into a separate DiagnosticIDs::Note diagnostic. ::: build/clang-plugin/tests/TestParamTraitsSpecializationHelper.cpp @@ +40,5 @@ > +{ > + typedef magic_int paramType; > +}; > + > +struct ContiguousEnumSerializer {}; Perhaps flesh out ContiguousEnumSerializer a bit more (e.g. adding some typedefs to it) to make sure that in the future we don't start getting false positives due to typedefs in the helper class.
Attachment #8906704 - Flags: feedback?(ehsan) → feedback+
Thank you very much Michael, your feedback is really helpful. I agree on the name, also much shorter and better, so I changed everything accordingly. I hope I also addressed all your nits correctly. I fleshed out the test to include a simplified version of serialization helpers in IPCMessageUtils.h (simplified meaning here to not include any Read/Write functions, just typedefs and general template declarations) > @@ +22,5 @@ > > + TypedefDecl *def = dyn_cast<TypedefDecl>(*iter); > > + QualType t = def->getUnderlyingType(); > > + QualType c = t.getCanonicalType(); > > + > > + const clang::Type *ptr = c.getTypePtrOrNull(); > > nit: I think that this file should already be `using namespace clang` so > clang:: isn't necessary. I tried to omit the clang:: namespace, however I got an error during compilation because the compiler couldn't decide if it is llvm::Type or clang::Type, so this WIP patch sill has the namespace. Asking again for feedback, also would we want this kind of static analysis? If so I am going to file bugs to fix existing places in the code base so we can land this. Thanks again Michael.
Attachment #8906704 - Attachment is obsolete: true
Attachment #8909244 - Flags: feedback?(michael)
Comment on attachment 8909244 [details] [diff] [review] Reject custom ParamTraits<> specializations for enum types Review of attachment 8909244 [details] [diff] [review]: ----------------------------------------------------------------- Looks awesome :-). I imagine that we will want something like this in the tree - small static analyses which catch easy-to-make mistakes are great.
Attachment #8909244 - Flags: feedback?(michael) → feedback+
Depends on: 1401107
Depends on: 1401111
Great, thanks Michael, I filed the bugs to fix the existing locations, then we could land this.
Comment on attachment 8909244 [details] [diff] [review] Reject custom ParamTraits<> specializations for enum types Hey Michael, are you the correct person to ask for review here?
Attachment #8909244 - Attachment description: [WIP/v2] Reject custom ParamTraits<> specializations for enums → Reject custom ParamTraits<> specializations for enum types
Attachment #8909244 - Flags: review?(michael)
Comment on attachment 8909244 [details] [diff] [review] Reject custom ParamTraits<> specializations for enum types Review of attachment 8909244 [details] [diff] [review]: ----------------------------------------------------------------- I am the right person to ask here :-) - One comment, but if those tests pass then this is good to go. ::: build/clang-plugin/tests/TestParamTraitsEnum.cpp @@ +12,5 @@ > +typedef enum { > + GoodFirst, > + GoodSecond, > + GoodLast > +} GoodEnum; Can you also try declaring an enum without using typedef (like `enum RawEnum {};`) and also check enum classes (`enum class EnumClass {};`), I want to check that they're also caught here.
Attachment #8909244 - Flags: review?(michael) → review+
Carry over r+ from Comment 7. Thanks Michael, I added the additional declarations and it compiles for me locally. Before requesting landing I will do a try push just to make sure it doesn't break anything.
Attachment #8909244 - Attachment is obsolete: true
Attachment #8910657 - Flags: review+
Priority: -- → P3
Assignee: julian.r.hector → nobody
What is needed to get this landed? It seems to have r+, was the try push all clear Julian? I.e Can we land this?
Flags: needinfo?(julian.r.hector)
Hey Paul, not quite yet, if we land this it will cause a build error. The two dependent bugs need to land first (those are the places that would fail) Then a try push should be done (build only should be sufficient) just to make sure. (I can't do that anymore since my try access has been revoked because I haven't pushed to try in a while)
Flags: needinfo?(julian.r.hector)
This would have also caught bug 1411631. Both of the two dependent bugs look straight forward to land, so hopefully we can get this done quickly.
Attached patch 1398874.patch — — Splinter Review
Same patch, with hg header and such. Carrying over the r+. Dan, can we drop the sec-hidden since there are no longer any instances of this pattern in the codebase?
Assignee: nobody → agaynor
Attachment #8910657 - Attachment is obsolete: true
Flags: needinfo?(dveditz)
Attachment #8935003 - Flags: review+
Keywords: checkin-needed
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla59
(In reply to Alex Gaynor [:Alex_Gaynor] from comment #12) > Dan, can we drop the sec-hidden since there are no longer any instances of > this pattern in the codebase? There are in shipping versions though, right? Bug 1411631 at least -- shouldn't we wait until we ship that?
Flags: needinfo?(dveditz) → needinfo?(agaynor)
Yes, excellent point; I was only looking at the two blocker bugs (neither of which were sec-).
Flags: needinfo?(agaynor)
Group: dom-core-security → core-security-release
Flags: qe-verify-
Whiteboard: [post-critsmash-triage]
Product: Core → Firefox Build System
Whiteboard: [post-critsmash-triage] → [post-critsmash-triage][adv-main59-]
Group: core-security-release
Product: Firefox Build System → Developer Infrastructure
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: