{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/osquery/pulls/comments/44376915","id":44376915,"diff_hunk":"@@ -0,0 +1,126 @@\n+/*\n+ *  Copyright (c) 2015, Facebook, Inc.\n+ *  All rights reserved.\n+ *\n+ *  This source code is licensed under the BSD-style license found in the\n+ *  LICENSE file in the root directory of this source tree. An additional grant\n+ *  of patent rights can be found in the PATENTS file in the same directory.\n+ *\n+ */\n+\n+#include <boost/filesystem.hpp>\n+#include <Cocoa/Cocoa.h>                /* For NSAppKitVersionNumber */\n+#include <Foundation/Foundation.h>\n+#include <Security/CodeSigning.h>\n+\n+#include <osquery/core.h>\n+#include <osquery/core/conversions.h>\n+#include <osquery/filesystem.h>\n+#include <osquery/logger.h>\n+#include <osquery/sql.h>\n+#include <osquery/tables.h>\n+\n+namespace osquery {\n+namespace tables {\n+\n+void genSignatureForFile(const std::string& path,\n+                         QueryData& results) {\n+  Row r;\n+  OSStatus result = 0;\n+  CFStringRef ident = NULL;\n+  CFDictionaryRef codeInfo = NULL;\n+  SecStaticCodeRef staticCode = NULL;\n+  SecCSFlags csFlags = kSecCSDefaultFlags;\n+\n+  // Defaults\n+  r[\"path\"] = path;\n+  r[\"signed\"] = INTEGER(0);\n+  r[\"identifier\"] = \"\";\n+\n+  // Create a URL that points to this file.\n+  auto url = (__bridge CFURLRef)[NSURL fileURLWithPath:@(path.c_str())];\n+  if (url == nullptr) {\n+    VLOG(1) << \"Could not create URL from file: \" << path;\n+    goto finally;\n+  }\n+\n+  // Create the static code object.\n+  result = SecStaticCodeCreateWithPath(url, kSecCSDefaultFlags, &staticCode);\n+  if (result != 0) {\n+    VLOG(1) << \"Could not create static code object for file: \" << path;\n+    goto finally;\n+  }\n+\n+  // Set up the flags - some of them aren't present on 10.8.\n+  if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_8) {\n+    csFlags = kSecCSDefaultFlags | kSecCSCheckNestedCode;\n+  }\n+  else {\n+    csFlags = kSecCSBasicValidateOnly;\n+  }\n+\n+  // Actually validate.\n+  result = SecStaticCodeCheckValidityWithErrors(staticCode, csFlags, NULL, NULL);\n+  if (result != 0) {\n+    // If this errors, then we either don't have a signature, or it's malformed.\n+    VLOG(2) << \"Static code validity check failed for file: \" << path;\n+    goto done;\n+  }\n+\n+  result = SecCodeCopySigningInformation(\n+    staticCode,\n+    kSecCSSigningInformation | kSecCSRequirementInformation,\n+    &codeInfo);\n+  if (result != 0) {\n+    VLOG(1) << \"Could not get signing information for file: \" << path;\n+    goto finally;\n+  }\n+\n+  // If we don't get an identifier for this file, then it's not signed.\n+  ident = (CFStringRef)CFDictionaryGetValue(codeInfo, kSecCodeInfoIdentifier);\n+  if (ident == nullptr) {\n+    VLOG(2) << \"No identifier found for file: \" << path;\n+    goto done;\n+  }\n+\n+  // We have an identifier - this indicates that the file is signed, and, since\n+  // it didn't error above, it's *also* a valid signature.\n+  r[\"signed\"] = INTEGER(1);\n+  r[\"identifier\"] = stringFromCFString(ident);\n+\n+  // TODO(andrew-d): can get more information from the signature here?\n+\n+  VLOG(3) << \"Everything succeeded for: \" << path;\n+\n+done:","path":"osquery/tables/system/darwin/signature.mm","position":95,"original_position":95,"commit_id":"6fbcef1d80c778b98852d6528edc91c8e671fcc1","original_commit_id":"6fbcef1d80c778b98852d6528edc91c8e671fcc1","user":{"login":"theopolis","id":981645,"avatar_url":"https://avatars.githubusercontent.com/u/981645?v=3","gravatar_id":"","url":"https://api.github.com/users/theopolis","html_url":"https://github.com/theopolis","followers_url":"https://api.github.com/users/theopolis/followers","following_url":"https://api.github.com/users/theopolis/following{/other_user}","gists_url":"https://api.github.com/users/theopolis/gists{/gist_id}","starred_url":"https://api.github.com/users/theopolis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theopolis/subscriptions","organizations_url":"https://api.github.com/users/theopolis/orgs","repos_url":"https://api.github.com/users/theopolis/repos","events_url":"https://api.github.com/users/theopolis/events{/privacy}","received_events_url":"https://api.github.com/users/theopolis/received_events","type":"User","site_admin":false},"body":"We should avoid `goto`s and the labels. It might make for a bit of duplication in release checking but that's fine. If we hit a breaking point we can switch to constructing unique pointers with custom destructors. ","created_at":"2015-11-10T07:34:56Z","updated_at":"2015-11-10T07:34:56Z","html_url":"https://github.com/facebook/osquery/pull/1642#discussion_r44376915","pull_request_url":"https://api.github.com/repos/facebook/osquery/pulls/1642","_links":{"self":{"href":"https://api.github.com/repos/facebook/osquery/pulls/comments/44376915"},"html":{"href":"https://github.com/facebook/osquery/pull/1642#discussion_r44376915"},"pull_request":{"href":"https://api.github.com/repos/facebook/osquery/pulls/1642"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/osquery/pulls/1642","id":50210478,"html_url":"https://github.com/facebook/osquery/pull/1642","diff_url":"https://github.com/facebook/osquery/pull/1642.diff","patch_url":"https://github.com/facebook/osquery/pull/1642.patch","issue_url":"https://api.github.com/repos/facebook/osquery/issues/1642","number":1642,"state":"open","locked":false,"title":"Add a `macho_signature` table","user":{"login":"andrew-d","id":1079173,"avatar_url":"https://avatars.githubusercontent.com/u/1079173?v=3","gravatar_id":"","url":"https://api.github.com/users/andrew-d","html_url":"https://github.com/andrew-d","followers_url":"https://api.github.com/users/andrew-d/followers","following_url":"https://api.github.com/users/andrew-d/following{/other_user}","gists_url":"https://api.github.com/users/andrew-d/gists{/gist_id}","starred_url":"https://api.github.com/users/andrew-d/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrew-d/subscriptions","organizations_url":"https://api.github.com/users/andrew-d/orgs","repos_url":"https://api.github.com/users/andrew-d/repos","events_url":"https://api.github.com/users/andrew-d/events{/privacy}","received_events_url":"https://api.github.com/users/andrew-d/received_events","type":"User","site_admin":false},"body":"This table allows verifying the signature of files (or bundles) on\r\nDarwin.  It also provides the signing identifier that is a part of the\r\nsignature.\r\n\r\nI've made a couple of possibly-controversial changes here, most important of which is adding a dependency on the Cocoa framework in order to get access to NSAppKitVersionNumber.  If this is something you'd rather not support, let me know and I'd be happy to strip that portion out.","created_at":"2015-11-10T03:31:50Z","updated_at":"2015-11-10T07:34:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d5dd7cf137087ea74db9ab3335d8f0686d157dfc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/facebook/osquery/pulls/1642/commits","review_comments_url":"https://api.github.com/repos/facebook/osquery/pulls/1642/comments","review_comment_url":"https://api.github.com/repos/facebook/osquery/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/osquery/issues/1642/comments","statuses_url":"https://api.github.com/repos/facebook/osquery/statuses/6fbcef1d80c778b98852d6528edc91c8e671fcc1","head":{"label":"stripe:andrew-add-codesign","ref":"andrew-add-codesign","sha":"6fbcef1d80c778b98852d6528edc91c8e671fcc1","user":{"login":"stripe","id":856813,"avatar_url":"https://avatars.githubusercontent.com/u/856813?v=3","gravatar_id":"","url":"https://api.github.com/users/stripe","html_url":"https://github.com/stripe","followers_url":"https://api.github.com/users/stripe/followers","following_url":"https://api.github.com/users/stripe/following{/other_user}","gists_url":"https://api.github.com/users/stripe/gists{/gist_id}","starred_url":"https://api.github.com/users/stripe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stripe/subscriptions","organizations_url":"https://api.github.com/users/stripe/orgs","repos_url":"https://api.github.com/users/stripe/repos","events_url":"https://api.github.com/users/stripe/events{/privacy}","received_events_url":"https://api.github.com/users/stripe/received_events","type":"Organization","site_admin":false},"repo":{"id":45881981,"name":"osquery","full_name":"stripe/osquery","owner":{"login":"stripe","id":856813,"avatar_url":"https://avatars.githubusercontent.com/u/856813?v=3","gravatar_id":"","url":"https://api.github.com/users/stripe","html_url":"https://github.com/stripe","followers_url":"https://api.github.com/users/stripe/followers","following_url":"https://api.github.com/users/stripe/following{/other_user}","gists_url":"https://api.github.com/users/stripe/gists{/gist_id}","starred_url":"https://api.github.com/users/stripe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stripe/subscriptions","organizations_url":"https://api.github.com/users/stripe/orgs","repos_url":"https://api.github.com/users/stripe/repos","events_url":"https://api.github.com/users/stripe/events{/privacy}","received_events_url":"https://api.github.com/users/stripe/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/stripe/osquery","description":"SQL powered operating system instrumentation, monitoring, and analytics.","fork":true,"url":"https://api.github.com/repos/stripe/osquery","forks_url":"https://api.github.com/repos/stripe/osquery/forks","keys_url":"https://api.github.com/repos/stripe/osquery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stripe/osquery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stripe/osquery/teams","hooks_url":"https://api.github.com/repos/stripe/osquery/hooks","issue_events_url":"https://api.github.com/repos/stripe/osquery/issues/events{/number}","events_url":"https://api.github.com/repos/stripe/osquery/events","assignees_url":"https://api.github.com/repos/stripe/osquery/assignees{/user}","branches_url":"https://api.github.com/repos/stripe/osquery/branches{/branch}","tags_url":"https://api.github.com/repos/stripe/osquery/tags","blobs_url":"https://api.github.com/repos/stripe/osquery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stripe/osquery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stripe/osquery/git/refs{/sha}","trees_url":"https://api.github.com/repos/stripe/osquery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stripe/osquery/statuses/{sha}","languages_url":"https://api.github.com/repos/stripe/osquery/languages","stargazers_url":"https://api.github.com/repos/stripe/osquery/stargazers","contributors_url":"https://api.github.com/repos/stripe/osquery/contributors","subscribers_url":"https://api.github.com/repos/stripe/osquery/subscribers","subscription_url":"https://api.github.com/repos/stripe/osquery/subscription","commits_url":"https://api.github.com/repos/stripe/osquery/commits{/sha}","git_commits_url":"https://api.github.com/repos/stripe/osquery/git/commits{/sha}","comments_url":"https://api.github.com/repos/stripe/osquery/comments{/number}","issue_comment_url":"https://api.github.com/repos/stripe/osquery/issues/comments{/number}","contents_url":"https://api.github.com/repos/stripe/osquery/contents/{+path}","compare_url":"https://api.github.com/repos/stripe/osquery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stripe/osquery/merges","archive_url":"https://api.github.com/repos/stripe/osquery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stripe/osquery/downloads","issues_url":"https://api.github.com/repos/stripe/osquery/issues{/number}","pulls_url":"https://api.github.com/repos/stripe/osquery/pulls{/number}","milestones_url":"https://api.github.com/repos/stripe/osquery/milestones{/number}","notifications_url":"https://api.github.com/repos/stripe/osquery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stripe/osquery/labels{/name}","releases_url":"https://api.github.com/repos/stripe/osquery/releases{/id}","created_at":"2015-11-10T02:28:01Z","updated_at":"2015-11-10T02:28:04Z","pushed_at":"2015-11-10T05:48:45Z","git_url":"git://github.com/stripe/osquery.git","ssh_url":"git@github.com:stripe/osquery.git","clone_url":"https://github.com/stripe/osquery.git","svn_url":"https://github.com/stripe/osquery","homepage":"https://osquery.io","size":16504,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"facebook:master","ref":"master","sha":"005c7d82e18e51117463fe92e47a4663ae21f2c5","user":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"repo":{"id":22394357,"name":"osquery","full_name":"facebook/osquery","owner":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/facebook/osquery","description":"SQL powered operating system instrumentation, monitoring, and analytics.","fork":false,"url":"https://api.github.com/repos/facebook/osquery","forks_url":"https://api.github.com/repos/facebook/osquery/forks","keys_url":"https://api.github.com/repos/facebook/osquery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/facebook/osquery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/facebook/osquery/teams","hooks_url":"https://api.github.com/repos/facebook/osquery/hooks","issue_events_url":"https://api.github.com/repos/facebook/osquery/issues/events{/number}","events_url":"https://api.github.com/repos/facebook/osquery/events","assignees_url":"https://api.github.com/repos/facebook/osquery/assignees{/user}","branches_url":"https://api.github.com/repos/facebook/osquery/branches{/branch}","tags_url":"https://api.github.com/repos/facebook/osquery/tags","blobs_url":"https://api.github.com/repos/facebook/osquery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/facebook/osquery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/facebook/osquery/git/refs{/sha}","trees_url":"https://api.github.com/repos/facebook/osquery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/facebook/osquery/statuses/{sha}","languages_url":"https://api.github.com/repos/facebook/osquery/languages","stargazers_url":"https://api.github.com/repos/facebook/osquery/stargazers","contributors_url":"https://api.github.com/repos/facebook/osquery/contributors","subscribers_url":"https://api.github.com/repos/facebook/osquery/subscribers","subscription_url":"https://api.github.com/repos/facebook/osquery/subscription","commits_url":"https://api.github.com/repos/facebook/osquery/commits{/sha}","git_commits_url":"https://api.github.com/repos/facebook/osquery/git/commits{/sha}","comments_url":"https://api.github.com/repos/facebook/osquery/comments{/number}","issue_comment_url":"https://api.github.com/repos/facebook/osquery/issues/comments{/number}","contents_url":"https://api.github.com/repos/facebook/osquery/contents/{+path}","compare_url":"https://api.github.com/repos/facebook/osquery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/facebook/osquery/merges","archive_url":"https://api.github.com/repos/facebook/osquery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/facebook/osquery/downloads","issues_url":"https://api.github.com/repos/facebook/osquery/issues{/number}","pulls_url":"https://api.github.com/repos/facebook/osquery/pulls{/number}","milestones_url":"https://api.github.com/repos/facebook/osquery/milestones{/number}","notifications_url":"https://api.github.com/repos/facebook/osquery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/facebook/osquery/labels{/name}","releases_url":"https://api.github.com/repos/facebook/osquery/releases{/id}","created_at":"2014-07-29T20:27:33Z","updated_at":"2015-11-10T07:11:29Z","pushed_at":"2015-11-10T05:48:46Z","git_url":"git://github.com/facebook/osquery.git","ssh_url":"git@github.com:facebook/osquery.git","clone_url":"https://github.com/facebook/osquery.git","svn_url":"https://github.com/facebook/osquery","homepage":"https://osquery.io","size":16504,"stargazers_count":5372,"watchers_count":5372,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":502,"mirror_url":null,"open_issues_count":24,"forks":502,"open_issues":24,"watchers":5372,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/osquery/pulls/1642"},"html":{"href":"https://github.com/facebook/osquery/pull/1642"},"issue":{"href":"https://api.github.com/repos/facebook/osquery/issues/1642"},"comments":{"href":"https://api.github.com/repos/facebook/osquery/issues/1642/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/osquery/pulls/1642/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/osquery/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/osquery/pulls/1642/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/osquery/statuses/6fbcef1d80c778b98852d6528edc91c8e671fcc1"}}}}