Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrandHogg detection #4498

Merged
merged 10 commits into from
Mar 3, 2022
Merged

StrandHogg detection #4498

merged 10 commits into from
Mar 3, 2022

Conversation

yostyle
Copy link
Contributor

@yostyle yostyle commented Nov 18, 2021

No description provided.

@yostyle yostyle requested a review from bmarty November 18, 2021 01:05
@github-actions
Copy link

github-actions bot commented Nov 18, 2021

Unit Test Results

  92 files  ±0    92 suites  ±0   1m 13s ⏱️ +7s
162 tests ±0  162 ✔️ ±0  0 💤 ±0  0 ±0 
524 runs  ±0  524 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit e9814ee. ± Comparison against base commit dd0d2e8.

♻️ This comment has been updated with latest results.

@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from 00bed50 to a3b4c31 Compare November 18, 2021 10:12
@yostyle yostyle marked this pull request as ready for review November 18, 2021 10:23
<activity android:name=".features.home.HomeActivity" />
<activity
android:name=".features.home.HomeActivity"
android:launchMode="singleTask" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding singleTask to this activity for a deeplink issue and it caused a bunch of odd behaviours (skipping deeplinks, activity not fully loading), have you noticed anything strange after the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see any problem on HomeActivity after this change but I didn't stress it. I would prefer to make more tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, clicking on a link like https://app.element.io/#/room/%23element-web-announcements:matrix.org

creates 2 tasks if Element was already running, so we can see in the task switcher:

image

See #2299 and the fix #2341 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a blocker for the fix? maybe singleTop could provide similar protection without allowing multiple instances

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more needed to change launchMode since we generate a random task affinity on each build.

@@ -42,5 +58,81 @@ class VectorActivityLifecycleCallbacks constructor(private val popupAlertManager
}

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
// restart the app if the task contains an unknown activity
if (isTaskCorrupted(activity)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be interesting to know if there's a performance impact for this, might be worth making the check async to avoid slowing down the activity start flow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -520,7 +521,16 @@ class HomeActivity :
if (views.drawerLayout.isDrawerOpen(GravityCompat.START)) {
views.drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R && supportFragmentManager.backStackEntryCount == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we could extract this logic to reduce the duplication, either extension or base class function

something like

if (views.drawerLayout.isDrawerOpen(GravityCompat.START)) { 
  views.drawerLayout.closeDrawer(GravityCompat.START)
} else {
  validateBackPressed { super.onBackPressed() }
}

...

fun Activity.validateBackPressed(onBackPressed: () -> Unit) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R && supportFragmentManager.backStackEntryCount == 0) {
        if (isTaskRoot) {
            onBackPressed()
        } else {
            Timber.e("Application is potentially corrupted by an unknown activity")
            finishAffinity()
        }
    } else {
        onBackPressed()
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/vector-im/element-android/pull/4498/files#r754454956

It's worth noting that the app seems robust to the StrandHogg vulnerability with the fix. I have the error log when I try the attack.

@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from a3b4c31 to e454fab Compare February 23, 2022 13:56
@github-actions
Copy link

github-actions bot commented Feb 23, 2022

Matrix SDK

Integration Tests Results:

  • [org.matrix.android.sdk.session]
    passed="25" failures="17" errors="0" skipped="2"
  • [org.matrix.android.sdk.account]
    passed="5" failures="0" errors="0" skipped="2"
  • [org.matrix.android.sdk.internal]
    passed="62" failures="1" errors="0" skipped="28"
  • [org.matrix.android.sdk.ordering]
    passed="16" failures="0" errors="0" skipped="0"
  • [org.matrix.android.sdk.PermalinkParserTest]
    passed="2" failures="0" errors="0" skipped="0"

@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from a08946e to 11c5769 Compare February 23, 2022 18:25
@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from 11c5769 to 647e22c Compare February 23, 2022 21:44
@@ -114,6 +116,19 @@ fun AppCompatActivity.hideKeyboard() {
currentFocus?.hideKeyboard()
}

fun AppCompatActivity.validateBackPressed(onBackPressed: () -> Unit) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation to explain that this should be only used for top-level activities. A developer which is not aware of this could think that it's a good practice to use it for all activities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny: I had the same mental path "A developer which is not aware of this could think that it's a good practice to use it for all activities". And now I see this comment in GitHub, and the comment in the code :)

@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from d29f58d to 885a861 Compare March 2, 2022 15:59
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the suffix to the taskAffinity of VectorCallActivity?


return alphaCharset[rnd.nextInt(alphaCharset.length())] +
rnd.with {
(1..7).collect { alphaNumCharset[nextInt(alphaNumCharset.length())] }.join()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using 8 chars in [a-z] is enough :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -91,6 +91,17 @@ static def getFdroidVersionSuffix() {
}
}

static def getTaskAffinitySuffix() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to generateTaskAffinitySuffix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -131,6 +136,9 @@ android {
// Required for sonar analysis
versionName "${versionMajor}.${versionMinor}.${versionPatch}-sonar"

// Generate a random app task affinity
manifestPlaceholders = [appTaskAffinitySuffix:"${generateTaskAffinitySuffix()}"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker since we're not using the build cache, wanted to highlight generating new values on each build/gradle configuration cycle may cause some of the android gradle tasks to become non cachable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, something else I did not think about.
We want to have stable builds, i.e. the same code generates the same binary (related: #842, and context in element-hq/riot-android#3131). It's important for F-Droid.
With this change it will not be the case anymore.
I have not idea how to handle that now.

Copy link
Contributor

@ouchadam ouchadam Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the short git hash for the suffix? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should propably compute the task affinity suffix from tags or commit hash ?

Copy link
Contributor Author

@yostyle yostyle Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the short git hash for the suffix? 🤔

We can't use it as is. taskAffinity has same format as namespace :

  • It must have at least two segments (one or more dots).
  • Each segment must start with a letter.
  • All characters must be alphanumeric or an underscore [a-zA-Z0-9_].

https://developer.android.com/studio/build/configure-app-module#set-application-id

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use the git hash as a seed for the random()

Copy link
Member

@bmarty bmarty Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea to compute a suffix using git hash and just remove forbidden chars (but is there any?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many having a appTaskAffinitySuffix like a${git_hash_8_digits} with a a prefix to ensure it's always starting with a letter would fit all our needs. WDYT @yostyle ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have gitRevision. I'm going to reuse it.

@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from feab80d to a12f9b7 Compare March 3, 2022 13:38
@yostyle yostyle force-pushed the yostyle/fix_strandhogg branch from a12f9b7 to e9814ee Compare March 3, 2022 13:40
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. As per my test, it's working well.
I will squash commits to clean up the history of this branch.

@bmarty bmarty merged commit 6e6b04c into develop Mar 3, 2022
@bmarty bmarty deleted the yostyle/fix_strandhogg branch March 3, 2022 14:05
onurays added a commit that referenced this pull request Mar 8, 2022
* develop: (392 commits)
  Missing import of at-Ignore annotation.
  FTUE - Choose a display picture (#5323)
  Ignore ThreadMessagingTest as it seems to cause other integration tests to fail.
  Maybe the file is here?
  I give up for the weekend
  Frustration at artifact handling vs what's in docs.
  Update the top bar in a room (#5213)
  Tweak upload/download of codecov xml file
  Address review points from adam
  Remove unneeded code, retaining a comment for how to exclude certain projects
  Merge pull request #5405 from vector-im/cgizard/ISSUE-5402
  Remove the printing of file name to the log as it's doubling up information.
  Remove exclusions (for now).
  Fix typo in name of action
  giving avatar/display name error dialogs human readable error messages - reuses the ErrorDialog logic which translates exceptions to human readable strings
  Run codecoverage and pass to sonarqube upload for processing.
  Better codecov based on ouchadam's suggestion
  Correct name of environment variable
  Use environment variable that is tied to project property
  Merge pull request #4498 from vector-im/yostyle/fix_strandhogg
  ...

# Conflicts:
#	vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants