Closed Bug 97614 Opened 25 years ago Closed 25 years ago

Crash when calling PK11_ResetToken repeatedly, with a non-empty-cert-db

Categories

(NSS :: Libraries, defect, P1)

x86
Linux
defect

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: inactive-mailbox, Assigned: rrelyea)

References

Details

Attachments

(4 files)

I' transfering this problem from 96018 to this new bug and repeat the problem description. I was using a cert database that was in use for a while, containing two private keys. I called PK11_ResetToken. The password and my certificates were removed. Then I called PK11_ResetToken again. It crashed. Stack of crash: #0 0x4176cda7 in hash4 (keyarg=0x89da5d6, len=4294963210) at ../../../mozilla/dbm/src/h_func.c:185 #1 0x4176a791 in __call_hash (hashp=0x82114d0, k=0x89da5d6 "±\217`[Û0ö\216ÐÕÚÚÚÚ)", len=4294963210) at ../../../mozilla/dbm/src/hash.c:1153 #2 0x41769ce0 in hash_access (hashp=0x82114d0, action=HASH_DELETE, key=0xbfffc858, val=0x0) at ../../../mozilla/dbm/src/hash.c:859 #3 0x41769c46 in hash_delete (dbp=0x8211ed0, key=0xbfffc858, flag=0) at ../../../mozilla/dbm/src/hash.c:819 #4 0x41719a49 in SECKEY_ResetKeyDB (handle=0x820e858) at keydb.c:2444 #5 0x4170337c in NSC_InitToken (slotID=2, pPin=0x0, ulPinLen=0, pLabel=0xbfffc8d4 "Software Security Device $Éÿ¿çLfAH\e\"\b") at pkcs11.c:2716 #6 0x416e8ff3 in PK11_ResetToken (slot=0x8221b48, sso_pwd=0x0) at pk11slot.c:4281 The error seems to happen inside function SECKEY_ResetKeyDB. On line ret = (* handle->db->del)(handle->db, &key, 0); the variable key contains {data = 0x89da5d6, size = 4294963210} which looks wrong. BTW, data contains a member with the a similar size (4294967286). The loop iterates a few times, until it arrives at this invalid key. The same crash happens reproducible when I restart the application and call PK11_ResetToken. I tested what happens when I have a fresh empty certificate database, i.e. the one created by default during init. With this db, I can call PK11_ResetToken multiple times without crashing.
Adding this bug as a blocker for 96018. -> P1
Blocks: 96018
Priority: -- → P1
I found a way how you can reproduce the problem. It might not be the same crash, but I think it's the same problem. To reproduce, you might want to apply the patch from bug 96018 to get the functionality to trigger PK11_ResetToken via the user interface. - erase your certificate database files - start mozilla - go to cert manager - restore a certificate from a p12 - create your new password as requested - enter the password protecting the p12 file - go to master passwords - click reset password - confirm reset & erase - go to cert manager - restore the same cert once again - create your new password as requested - enter the password protecting the p12 file - crash
Assigned the bug to Bob.
Assignee: wtc → relyea
OK, I've found out a couple of things.... First I can't seem to reproduce the crash with my command line tools. I do see a problem with a second import of the cert and key if the cert already lives in the database, and I have a simple fix for that (I'll attach a bug). I'm wondering if part of the problem here is with my command line tooks I never try to reset the database while it is logged in. One thing -- where this bug is crashing, we shouldn't crash even if the database is hosed. Kai, and you attach the key3.db that causes the crash. I should be able to reproduce that crash with my tools. bob
After your request to produce a damaged key3.db, I just did some more testing. I repeated my step-by-step instructions from 2001-08-31 02:26. If I do, I still see the crash. To make sure this really is caused by a damaged cert database, I split the steps. I stoped after the call to ResetToken, and restarted the application. I then imported the cert. And it worked! I conclude that not the cert database files are damaged. It is the in-memory representation that seems to have problems. With the new knowledge, I tried to reproduce what I reported initially in this bug. I still have this cert database files, so I can easily try again. Up to now, I wasn't able to reproduce that exactly. I will continue to try, as I'm sure that in my initial report I really had a damaged database, but currently I have not. However, during my tests I saw different unexpected results (and crashes). Right now I'm unwilling to attach the cert database files I've seen the problems with, because they contain real life private keys... I therefore made additional tests to reproduce using certificates that I can give you. Problem 1 ========= If I try to import a cert, but type in the wrong p12 file password, it crashes. This does happen with one of my certificates. It does not happen with all of them. I will research this more. I'm not sure if this was related to my previous crashes. I could send you this certificate. Problem 2 ========= Erase cert db files. Start app. Import a cert, e.g. your Netscape Intranet cert. Call ResetToken. Import the same cert again. Crash. The following works: Erase cert db files. Start app. Import cert. Call ResetToken. Restart app. Again import, reset, restart. I can repeat this cycle as often as I want, no crash. I can even call Reset repeatedly, no crash. If I do every action in a separate session, I see no crash with the Netscape Intranet cert. Bob, this might explain why you cannot reproduce with the NSS tools, as you create a new session for each action. I tried to change the order, i.e. import, restart, reset, import, restart reset, etc., this does work too. I need to import twice within one session to see this problem, therefore it must be in-memory corruption. Can you produce something like a shell mode for the tools, where you can execute multiple actions within the same session. I hope you will be able to reproduce this using your own Netscape certificate. Problem 3 ========= I have a cert that is expired AND is from an unknown CA. As I haven't used this cert for anything, I'm willing to send you this file by private mail (encrypted). Erase cert dbs. Import, success. Restart app. ResetToken. Restart app. Import, crash. This is the only case that I have hope you might be able to reproduce with the tools. In addition to the p12 file containing the cert, I'll send you the *.db files from the Mozilla profile, in the state before I start the application where importing causes the crash.
Sigh, I'm still having problems reproducing these crashes. I've tried all my certs, including expired unkown certs and I can't get it to crash. I have found some weirdness, which I've included a patch for.
Maybe this is a side effect using the cert manager application code? Have you tried doing multiple actions within one application session?
I applied your new patch (the second hunk from your first patch is already contained in the current Mozilla trunk and 094 branch). This changes things a bit, but it didn't get better. If I try problem 3 with the same (expired) cert I initially used, it no longer crashes. However, when I do import, restart, reset, restart, import, this second import doesn't work and I get the error message "the pkcs #12 operation failed for unknown reasons", which means we saw PIP_PKCS12_NSS_ERROR (or a different unhandled error in PSM). But if I try problem 3 with a different cert, for example my AOL/Netscape cert, I still get a crash. Although this time it is not a segmentation violation, but an assert get's triggered: Assertion failure: !cert->isperm, at pcertdb.c:4794
Oops, I did the diff by hand and reversed the order. The second half of the first patch needs to add the code in the '-' part and remove the code in the '+'. This code is to prevent the 'isPerm' crash. bob
Ok, I applied the missing patch. The assert/abort is no longer triggered. When I now import my AOL/Netscape cert, and if I restrict myself to one action (either import or reset) per session, I don't see the crash anymore. However, as soon as I import twice within the same session, I crash. I start to suspect whether the certificate manager application code might influence this. I noticed: Whenever I import a cert and then exit the application, the XPCom system complains that the reference count has not arrived at zero. (Even if this is not the cause, we must enhance our patch further. As our dialogs are not modal, a user could have the cert manager window open, go back to preferences window and press reset. We must make sure that everything get's erased from the open cert manager view before we call erase. However, in my tests, where I see the crashes, I always closed cert manager before pressing reset. I just hope that opening the cert manager does not trigger some certs to be cached in memory, which could explain the crash.) Ok, but the other three problems are still there. - I still crash if I do import, reset, import within one session. - I get the error message when trying to import the expired cert the second time, with each import, reset, import in a separate session. - I still get the crash when I try to import one of my certs into a fresh database and enter a wrong p12 password. This does not crash with the AOL/Netscape cert.
A couple of observations. The number 4294963210 is an unsigned 32-bit number. In hex it is 0xFFFFF00A If we interpret that value as a signed 32-bit number, it evaluates to -4086. Likewise, the number 4294967286 in hex is 0xFFFFFFF6, and when interpreted as a signed number, it is -10. So, there's apparently some code that's subtracting numbers from an unsigned value, and is not checking for overflow.
Here's the patch of issue one... it's been checked in to both the NSS tip and the NSS 3.3 branch (not to be confused by the mozilla trunk and branch).
OK, I have a new patch which should fix the remaining problems, including the initial crash reported. The idea of the patch is instead of walking down the database, trying to delete entries of a possibly corrupted DB, we simple close the database, then reopen the database O_TRUNC and reinitialize it.
requesting review, approval to: 1) move the NNS_CLIENT_TAG to the new version. 2) when the trunk build has been validated, merge the tagged version onto the 0.9.4 branch. This would be done together with bug 96018.
Kai, can you apply this last patch and make sure all your crashes except the 'import into fresh database with bad password' case all work now. The last one is a separate bug independent of the key reset problem. bob
I can confirm that it is exactly as you said! Great work.
Adding review keyword. We request permission to move the NSS_CLIENT_TAG to the fixed version of the affected files on the NSS 3.3 branch. Note that the fix is checked in, but not used by mozilla until the tag is moved. Tag move is needed to unblock 96018.
Keywords: review
According to http://www.mozilla.org/hacking/reviewers.html we can move the tag with peer and module owner approval (wtc). David Drinan will move the tag.
I'm closing this bug, as David moved the tag and it works now for me.
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Todays NSS_CLIENT_TAG merged into 094 branch.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: