- Revision
- 270866
- Author
- [email protected]
- Date
- 2020-12-15 14:48:29 -0800 (Tue, 15 Dec 2020)
Log Message
Cherry-pick r270559. rdar://problem/72320712
Potential crash under [WKRemoteObjectEncoder encodeObject:forKey:] when the object graph contains a cycle
https://bugs.webkit.org/show_bug.cgi?id=219620
Source/WebKit:
<rdar://71551776>
Reviewed by Geoffrey Garen.
Update WKRemoteObjectEncoder to detect cycles when encoding objects. When a cycle is detected, we
first attempt to encode a default-initialized object of the same type instead. If that fails, we
raise a NSInvalidArgumentException.
Based on crashes in the wild, we have evidence that such cycles are occuring and I suspect this is
caused by Norton Safe Web extension somehow.
* Shared/API/Cocoa/WKRemoteObjectCoder.mm:
(-[WKRemoteObjectEncoder init]):
(encodeObject):
Tools:
Reviewed by Geoffrey Garen.
Add API test coverage.
* TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h:
* TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm:
(TEST):
* TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm:
(-[RemoteObjectRegistryPlugIn takeDictionary:completionHandler:]):
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@270559 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Modified Paths
Diff
Modified: branches/safari-610-branch/Source/WebKit/ChangeLog (270865 => 270866)
--- branches/safari-610-branch/Source/WebKit/ChangeLog 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Source/WebKit/ChangeLog 2020-12-15 22:48:29 UTC (rev 270866)
@@ -1,3 +1,60 @@
+2020-12-15 Alan Coon <[email protected]>
+
+ Cherry-pick r270559. rdar://problem/72320712
+
+ Potential crash under [WKRemoteObjectEncoder encodeObject:forKey:] when the object graph contains a cycle
+ https://bugs.webkit.org/show_bug.cgi?id=219620
+ Source/WebKit:
+
+ <rdar://71551776>
+
+ Reviewed by Geoffrey Garen.
+
+ Update WKRemoteObjectEncoder to detect cycles when encoding objects. When a cycle is detected, we
+ first attempt to encode a default-initialized object of the same type instead. If that fails, we
+ raise a NSInvalidArgumentException.
+
+ Based on crashes in the wild, we have evidence that such cycles are occuring and I suspect this is
+ caused by Norton Safe Web extension somehow.
+
+ * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
+ (-[WKRemoteObjectEncoder init]):
+ (encodeObject):
+
+ Tools:
+
+ Reviewed by Geoffrey Garen.
+
+ Add API test coverage.
+
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h:
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm:
+ (TEST):
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm:
+ (-[RemoteObjectRegistryPlugIn takeDictionary:completionHandler:]):
+
+
+ git-svn-id: https://svn.webkit.org/repository/webkit/trunk@270559 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+ 2020-12-08 Chris Dumez <[email protected]>
+
+ Potential crash under [WKRemoteObjectEncoder encodeObject:forKey:] when the object graph contains a cycle
+ https://bugs.webkit.org/show_bug.cgi?id=219620
+ <rdar://71551776>
+
+ Reviewed by Geoffrey Garen.
+
+ Update WKRemoteObjectEncoder to detect cycles when encoding objects. When a cycle is detected, we
+ first attempt to encode a default-initialized object of the same type instead. If that fails, we
+ raise a NSInvalidArgumentException.
+
+ Based on crashes in the wild, we have evidence that such cycles are occuring and I suspect this is
+ caused by Norton Safe Web extension somehow.
+
+ * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
+ (-[WKRemoteObjectEncoder init]):
+ (encodeObject):
+
2020-12-01 Alan Coon <[email protected]>
Cherry-pick r270072. rdar://problem/71836681
Modified: branches/safari-610-branch/Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm (270865 => 270866)
--- branches/safari-610-branch/Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm 2020-12-15 22:48:29 UTC (rev 270866)
@@ -31,10 +31,12 @@
#import "APIDictionary.h"
#import "APINumber.h"
#import "APIString.h"
+#import "Logging.h"
#import "NSInvocationSPI.h"
#import "_WKRemoteObjectInterfaceInternal.h"
#import <objc/runtime.h>
#import <wtf/RetainPtr.h>
+#import <wtf/Scope.h>
#import <wtf/SetForScope.h>
#import <wtf/text/CString.h>
@@ -61,6 +63,7 @@
API::Array* _objectStream;
API::Dictionary* _currentDictionary;
+ RetainPtr<NSMutableSet> _objectsBeingEncoded; // Used to detect cycles.
}
- (id)init
@@ -70,6 +73,7 @@
_rootDictionary = API::Dictionary::create();
_currentDictionary = _rootDictionary.get();
+ _objectsBeingEncoded = adoptNS([[NSMutableSet alloc] init]);
return self;
}
@@ -305,6 +309,22 @@
if (!objectClass)
[NSException raise:NSInvalidArgumentException format:@"-classForCoder returned nil for %@", object];
+ if ([encoder->_objectsBeingEncoded containsObject:object]) {
+ RELEASE_LOG_FAULT(IPC, "WKRemoteObjectCode::encodeObject: Object of type '%{private}s' contains a cycle", class_getName(object_getClass(object)));
+ @try {
+ // Try to encode a newly initialized object instead.
+ id newObject = [[[[object class] alloc] init] autorelease];
+ object = newObject;
+ } @catch (NSException *e) {
+ [NSException raise:NSInvalidArgumentException format:@"Object of type '%s' contains a cycle", class_getName(object_getClass(object))];
+ }
+ }
+
+ [encoder->_objectsBeingEncoded addObject:object];
+ auto exitScope = makeScopeExit([encoder, object] {
+ [encoder->_objectsBeingEncoded removeObject:object];
+ });
+
encoder->_currentDictionary->set(classNameKey, API::String::create(class_getName(objectClass)));
if ([object isKindOfClass:[NSInvocation class]]) {
Modified: branches/safari-610-branch/Tools/ChangeLog (270865 => 270866)
--- branches/safari-610-branch/Tools/ChangeLog 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Tools/ChangeLog 2020-12-15 22:48:29 UTC (rev 270866)
@@ -1,3 +1,56 @@
+2020-12-15 Alan Coon <[email protected]>
+
+ Cherry-pick r270559. rdar://problem/72320712
+
+ Potential crash under [WKRemoteObjectEncoder encodeObject:forKey:] when the object graph contains a cycle
+ https://bugs.webkit.org/show_bug.cgi?id=219620
+ Source/WebKit:
+
+ <rdar://71551776>
+
+ Reviewed by Geoffrey Garen.
+
+ Update WKRemoteObjectEncoder to detect cycles when encoding objects. When a cycle is detected, we
+ first attempt to encode a default-initialized object of the same type instead. If that fails, we
+ raise a NSInvalidArgumentException.
+
+ Based on crashes in the wild, we have evidence that such cycles are occuring and I suspect this is
+ caused by Norton Safe Web extension somehow.
+
+ * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
+ (-[WKRemoteObjectEncoder init]):
+ (encodeObject):
+
+ Tools:
+
+ Reviewed by Geoffrey Garen.
+
+ Add API test coverage.
+
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h:
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm:
+ (TEST):
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm:
+ (-[RemoteObjectRegistryPlugIn takeDictionary:completionHandler:]):
+
+
+ git-svn-id: https://svn.webkit.org/repository/webkit/trunk@270559 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+ 2020-12-08 Chris Dumez <[email protected]>
+
+ Potential crash under [WKRemoteObjectEncoder encodeObject:forKey:] when the object graph contains a cycle
+ https://bugs.webkit.org/show_bug.cgi?id=219620
+
+ Reviewed by Geoffrey Garen.
+
+ Add API test coverage.
+
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h:
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm:
+ (TEST):
+ * TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm:
+ (-[RemoteObjectRegistryPlugIn takeDictionary:completionHandler:]):
+
2020-12-01 Alan Coon <[email protected]>
Cherry-pick r270072. rdar://problem/71836681
Modified: branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h (270865 => 270866)
--- branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.h 2020-12-15 22:48:29 UTC (rev 270866)
@@ -48,6 +48,7 @@
- (void)takeLongLong:(long long)value completionHandler:(void (^)(long long value))completionHandler;
- (void)takeUnsignedLong:(unsigned long)value completionHandler:(void (^)(unsigned long value))completionHandler;
- (void)takeLong:(long)value completionHandler:(void (^)(long value))completionHandler;
+- (void)takeDictionary:(NSDictionary *)value completionHandler:(void (^)(NSDictionary *value))completionHandler;
- (void)doNotCallCompletionHandler:(void (^)())completionHandler;
- (void)sendRequest:(NSURLRequest *)request response:(NSURLResponse *)response challenge:(NSURLAuthenticationChallenge *)challenge error:(NSError *)error completionHandler:(void (^)(NSURLRequest *, NSURLResponse *, NSURLAuthenticationChallenge *, NSError *))completionHandler;
- (void)callUIProcessMethodWithReplyBlock;
Modified: branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm (270865 => 270866)
--- branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistry.mm 2020-12-15 22:48:29 UTC (rev 270866)
@@ -126,6 +126,51 @@
isDone = false;
+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://webkit.org/"]];
+ NSHTTPURLResponse *response = [[[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://webkit.org/"] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"testFieldName" : @"testFieldValue" }] autorelease];
+ NSError *error = [NSError errorWithDomain:@"testDomain" code:123 userInfo:@{@"a":@"b"}];
+ NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:@"testHost" port:80 protocol:@"testProtocol" realm:@"testRealm" authenticationMethod:NSURLAuthenticationMethodHTTPDigest] autorelease];
+ NSURLCredential *credential = [NSURLCredential credentialWithUser:@"testUser" password:@"testPassword" persistence:NSURLCredentialPersistenceForSession];
+ id<NSURLAuthenticationChallengeSender> sender = nil;
+ NSURLAuthenticationChallenge *challenge = [[[NSURLAuthenticationChallenge alloc] initWithProtectionSpace:protectionSpace proposedCredential:credential previousFailureCount:42 failureResponse:response error:error sender:sender] autorelease];
+ [object sendRequest:request response:response challenge:challenge error:error completionHandler:^(NSURLRequest *deserializedRequest, NSURLResponse *deserializedResponse, NSURLAuthenticationChallenge *deserializedChallenge, NSError *deserializedError) {
+ EXPECT_WK_STREQ(deserializedRequest.URL.absoluteString, "https://webkit.org/");
+ EXPECT_WK_STREQ([(NSHTTPURLResponse *)deserializedResponse allHeaderFields][@"testFieldName"], "testFieldValue");
+ EXPECT_WK_STREQ(deserializedChallenge.protectionSpace.realm, "testRealm");
+ EXPECT_WK_STREQ(deserializedError.domain, "testDomain");
+ isDone = true;
+ }];
+ TestWebKitAPI::Util::run(&isDone);
+
+ isDone = false;
+
+ bool exceptionThrown = false;
+ NSMutableDictionary *child = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"foo", @"name", [NSNumber numberWithInt:1], @"value", nil];
+ NSMutableDictionary *dictionaryWithCycle = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"root", @"name", child, @"child", nil];
+ [child setValue:dictionaryWithCycle forKey:@"parent"]; // Creates a cycle.
+ @try {
+ [object takeDictionary:dictionaryWithCycle completionHandler:^(NSDictionary* value) {
+ NSString *name = [value objectForKey:@"name"];
+ EXPECT_WK_STREQ(@"root", name);
+ NSDictionary *child = [value objectForKey:@"child"];
+ EXPECT_TRUE(!!child);
+ NSString* childName = [child objectForKey:@"name"];
+ EXPECT_WK_STREQ(@"foo", childName);
+ NSNumber *childValue = [child objectForKey:@"value"];
+ EXPECT_EQ(1, [childValue integerValue]);
+ // We should have encoded parent as an empty NSDictionary.
+ NSDictionary *childParent = [child objectForKey:@"parent"];
+ EXPECT_TRUE(!!childParent);
+ EXPECT_EQ(0U, [childParent count]);
+ isDone = true;
+ }];
+ TestWebKitAPI::Util::run(&isDone);
+ isDone = false;
+ } @catch (NSException *e) {
+ exceptionThrown = true;
+ }
+ EXPECT_FALSE(exceptionThrown);
+
class DoneWhenDestroyed : public RefCounted<DoneWhenDestroyed> {
public:
DoneWhenDestroyed(bool& isDone)
@@ -142,22 +187,6 @@
}
TestWebKitAPI::Util::run(&isDone);
-
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://webkit.org/"]];
- NSHTTPURLResponse *response = [[[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://webkit.org/"] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"testFieldName" : @"testFieldValue" }] autorelease];
- NSError *error = [NSError errorWithDomain:@"testDomain" code:123 userInfo:@{@"a":@"b"}];
- NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:@"testHost" port:80 protocol:@"testProtocol" realm:@"testRealm" authenticationMethod:NSURLAuthenticationMethodHTTPDigest] autorelease];
- NSURLCredential *credential = [NSURLCredential credentialWithUser:@"testUser" password:@"testPassword" persistence:NSURLCredentialPersistenceForSession];
- id<NSURLAuthenticationChallengeSender> sender = nil;
- NSURLAuthenticationChallenge *challenge = [[[NSURLAuthenticationChallenge alloc] initWithProtectionSpace:protectionSpace proposedCredential:credential previousFailureCount:42 failureResponse:response error:error sender:sender] autorelease];
- [object sendRequest:request response:response challenge:challenge error:error completionHandler:^(NSURLRequest *deserializedRequest, NSURLResponse *deserializedResponse, NSURLAuthenticationChallenge *deserializedChallenge, NSError *deserializedError) {
- EXPECT_WK_STREQ(deserializedRequest.URL.absoluteString, "https://webkit.org/");
- EXPECT_WK_STREQ([(NSHTTPURLResponse *)deserializedResponse allHeaderFields][@"testFieldName"], "testFieldValue");
- EXPECT_WK_STREQ(deserializedChallenge.protectionSpace.realm, "testRealm");
- EXPECT_WK_STREQ(deserializedError.domain, "testDomain");
- isDone = true;
- }];
- TestWebKitAPI::Util::run(&isDone);
}
}
Modified: branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm (270865 => 270866)
--- branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm 2020-12-15 22:48:10 UTC (rev 270865)
+++ branches/safari-610-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/RemoteObjectRegistryPlugIn.mm 2020-12-15 22:48:29 UTC (rev 270866)
@@ -104,6 +104,11 @@
completionHandler(value);
}
+- (void)takeDictionary:(NSDictionary *)value completionHandler:(void (^)(NSDictionary *value))completionHandler
+{
+ completionHandler(value);
+}
+
- (void)doNotCallCompletionHandler:(void (^)())completionHandler
{
}