Skip to content

Conversation

@RaisinTen
Copy link
Contributor

@RaisinTen RaisinTen commented Jan 14, 2022

When PEM_write_bio_PKCS8PrivateKey() and PEM_write_bio_PKCS8PrivateKey_nid()
were passed empty passphrase strings, OPENSSL_memdup() was incorrectly getting
used for 0 bytes size allocations, which resulted in malloc failures.

Fixes: #17506

Signed-off-by: Darshan Sen [email protected]

Checklist
  • documentation is added or updated
  • tests are added or updated

@openssl-machine openssl-machine added the hold: cla required The contributor needs to submit a license agreement label Jan 14, 2022
@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label Jan 14, 2022
@t8m
Copy link
Member

t8m commented Jan 14, 2022

In general this is wrong place to fix your problem. This needs to be properly handled at the callers to not try 0 bytes size allocation or memory duplication.

@RaisinTen RaisinTen force-pushed the fix-invalid-malloc-failures branch from b7dcbd1 to 3df2ce3 Compare January 15, 2022 12:06
@RaisinTen RaisinTen changed the title Fix invalid malloc failures Fix invalid malloc failures in PEM_write_bio_PKCS8PrivateKey() Jan 15, 2022
@RaisinTen RaisinTen force-pushed the fix-invalid-malloc-failures branch 2 times, most recently from 167f7d1 to ee25717 Compare January 15, 2022 12:20
@RaisinTen
Copy link
Contributor Author

In general this is wrong place to fix your problem. This needs to be properly handled at the callers to not try 0 bytes size allocation or memory duplication.

@t8m thanks, updated accordingly.

@RaisinTen RaisinTen force-pushed the fix-invalid-malloc-failures branch from ee25717 to 0ed5713 Compare January 15, 2022 14:35
@t8m t8m added branch: 3.0 Applies to openssl-3.0 branch branch: master Applies to master branch triaged: bug The issue/pr is/fixes a bug labels Jan 18, 2022
t8m
t8m previously approved these changes Jan 18, 2022
Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

LGTM. To be able to accept the PR we will need a CLA signed from you. Please follow the process described at https://www.openssl.org/policies/cla.html

@t8m t8m added the approval: review pending This pull request needs review by a committer label Jan 18, 2022
@t8m t8m dismissed their stale review January 18, 2022 10:49

Too early

@t8m
Copy link
Member

t8m commented Jan 18, 2022

The CI failures are relevant.

@t8m t8m removed the approval: review pending This pull request needs review by a committer label Jan 18, 2022
@RaisinTen RaisinTen force-pushed the fix-invalid-malloc-failures branch from 0ed5713 to 2a0f6be Compare January 18, 2022 14:04
@RaisinTen
Copy link
Contributor Author

@t8m it's just the callback variation that fails which was happening even before this change was added. I'm suspecting that's happening due to an incorrect invocation of the API? (suggestions would be great!) I've commented out that test for now with a TODO. If CI passes now (not sure why these tests aren't run for me locally, will investigate later) and y'all are good with the change, I'll try to sign the CLA.

@RaisinTen RaisinTen force-pushed the fix-invalid-malloc-failures branch from 2a0f6be to 00a1dad Compare January 18, 2022 14:13
@github-actions github-actions bot removed the severity: fips change The pull request changes FIPS provider sources label Jan 18, 2022
@t8m
Copy link
Member

t8m commented Jan 20, 2022

Please re-enable the last test case. The error can be fixed by changing https://github.com/openssl/openssl/blob/master/crypto/ui/ui_util.c#L117 to if (len < 0)

@t8m
Copy link
Member

t8m commented Jan 20, 2022

Also please sign and send the CLA.

@bernd-edlinger
Copy link
Member

Please re-enable the last test case. The error can be fixed by changing https://github.com/openssl/openssl/blob/master/crypto/ui/ui_util.c#L117 to if (len < 0)

but please be careful, return values 0 and -1 have a special meaning,
and the user callback has no way to return 0 any more with this change.
this is the caller:

openssl/crypto/ui/ui_lib.c

Lines 516 to 530 in 5288303

switch (ui->meth->ui_read_string(ui,
sk_UI_STRING_value(ui->strings,
i))) {
case -1: /* Interrupt/Cancel/something... */
ui->flags &= ~UI_FLAG_REDOABLE;
ok = -2;
goto err;
case 0: /* Errors */
state = "reading strings";
ok = -1;
goto err;
default: /* Success */
ok = 0;
break;
}

and this is the callee:

openssl/crypto/ui/ui_util.c

Lines 111 to 121 in 5288303

int len = data->cb(result,
maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
data->rwflag, UI_get0_user_data(ui));
if (len >= 0)
result[len] = '\0';
if (len <= 0)
return len;
if (UI_set_result_ex(ui, uis, result, len) >= 0)
return 1;
return 0;

@t8m
Copy link
Member

t8m commented Jan 21, 2022

@bernd-edlinger the callback is the pem callback which returns 0 if the password is zero length, not that the callback has failed. We should not return 0 from ui_read() if the pem callback did not fail but simply returned 0 length password.
The ui_read() will return 0 if UI_set_result_ex() fails, and that is correct.
So I am sure that the change I propose is correct.

@bernd-edlinger bernd-edlinger added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Jan 25, 2022
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Jan 26, 2022
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@t8m t8m removed the branch: 3.0 Applies to openssl-3.0 branch label Jan 26, 2022
openssl-machine pushed a commit that referenced this pull request Jan 26, 2022
When `PEM_write_bio_PKCS8PrivateKey()` was passed an empty passphrase
string, `OPENSSL_memdup()` was incorrectly getting used for 0 bytes size
allocation, which resulted in malloc failures.

Fixes: #17506

Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #17507)
openssl-machine pushed a commit that referenced this pull request Jan 26, 2022
Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #17507)
@t8m t8m added the branch: 3.0 Applies to openssl-3.0 branch label Jan 26, 2022
openssl-machine pushed a commit that referenced this pull request Jan 26, 2022
When `PEM_write_bio_PKCS8PrivateKey()` was passed an empty passphrase
string, `OPENSSL_memdup()` was incorrectly getting used for 0 bytes size
allocation, which resulted in malloc failures.

Fixes: #17506

Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #17507)

(cherry picked from commit 59ccb72)
openssl-machine pushed a commit that referenced this pull request Jan 26, 2022
Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #17507)

(cherry picked from commit 1d28ada)
@t8m
Copy link
Member

t8m commented Jan 26, 2022

Merged to master and 3.0 branches (resolving the CHANGES.md conflict manually). Thank you for your contribution.

@RaisinTen
Copy link
Contributor Author

@t8m thanks! Should this PR be closed, now that my commits have landed on the relevant branches or is there anything else I can help out with?

@t8m t8m closed this Jan 27, 2022
@RaisinTen RaisinTen deleted the fix-invalid-malloc-failures branch January 27, 2022 17:31
RaisinTen added a commit to RaisinTen/openssl that referenced this pull request Jan 29, 2022
When `PEM_write_bio_PKCS8PrivateKey()` was passed an empty passphrase
string, `OPENSSL_memdup()` was incorrectly getting used for 0 bytes size
allocation, which resulted in malloc failures.

Fixes: openssl#17506

Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#17507)
RaisinTen added a commit to RaisinTen/openssl that referenced this pull request Jan 29, 2022
Signed-off-by: Darshan Sen <[email protected]>

Reviewed-by: Bernd Edlinger <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#17507)
RaisinTen added a commit to RaisinTen/node that referenced this pull request Mar 13, 2022
openssl/openssl@59ccb72:

```
commit 59ccb72cd5cec3b4e312853621e12a68dacdbc7e
Author: Darshan Sen <[email protected]>
Date:   Fri Jan 14 16:22:41 2022 +0530

    Fix invalid malloc failures in PEM_write_bio_PKCS8PrivateKey()

    When `PEM_write_bio_PKCS8PrivateKey()` was passed an empty passphrase
    string, `OPENSSL_memdup()` was incorrectly getting used for 0 bytes size
    allocation, which resulted in malloc failures.

    Fixes: openssl/openssl#17506

    Signed-off-by: Darshan Sen <[email protected]>

    Reviewed-by: Bernd Edlinger <[email protected]>
    Reviewed-by: Paul Dale <[email protected]>
    Reviewed-by: Tomas Mraz <[email protected]>
    (Merged from openssl/openssl#17507)
```

openssl/openssl@1d28ada:

```
commit 1d28ada1c39997c10fe5392f4235bbd2bc44b40f
Author: Darshan Sen <[email protected]>
Date:   Sat Jan 22 17:56:05 2022 +0530

    Allow empty passphrase in PEM_write_bio_PKCS8PrivateKey_nid()

    Signed-off-by: Darshan Sen <[email protected]>

    Reviewed-by: Bernd Edlinger <[email protected]>
    Reviewed-by: Paul Dale <[email protected]>
    Reviewed-by: Tomas Mraz <[email protected]>
    (Merged from openssl/openssl#17507)
```

Refs: openssl/openssl#17507
Fixes: nodejs#41428
Signed-off-by: Darshan Sen <[email protected]>
RaisinTen added a commit to RaisinTen/node that referenced this pull request Mar 17, 2022
nodejs-github-bot pushed a commit to nodejs/node that referenced this pull request Mar 19, 2022
Refs: openssl/openssl#17507
Refs: #41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
bengl pushed a commit to nodejs/node that referenced this pull request Mar 21, 2022
Refs: openssl/openssl#17507
Refs: #41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
danielleadams pushed a commit to danielleadams/node that referenced this pull request Apr 21, 2022
Refs: openssl/openssl#17507
Refs: nodejs#41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: nodejs#42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
danielleadams pushed a commit to nodejs/node that referenced this pull request Apr 24, 2022
Refs: openssl/openssl#17507
Refs: #41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
danielleadams pushed a commit to nodejs/node that referenced this pull request Apr 24, 2022
Refs: openssl/openssl#17507
Refs: #41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
danielleadams pushed a commit to nodejs/node that referenced this pull request Apr 24, 2022
Refs: openssl/openssl#17507
Refs: #41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
xtx1130 pushed a commit to xtx1130/node that referenced this pull request Apr 25, 2022
Refs: openssl/openssl#17507
Refs: nodejs#41428
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: nodejs#42319
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch branch: 3.0 Applies to openssl-3.0 branch triaged: bug The issue/pr is/fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PEM_write_bio_PKCS8PrivateKey() doesn't accept empty passphrase strings anymore

6 participants