Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

[Pal/lib] Remove list-test, move path.c, refactor crypto code #2367

Merged
merged 3 commits into from
May 14, 2021

Conversation

dimakuv
Copy link
Contributor

@dimakuv dimakuv commented May 11, 2021

Description of the changes

One more PR in the series of Pal/lib refactoring.

Three commits here:

  • [Pal/lib] Remove unused list-test.c

  • [Pal/lib] Move Pal/lib/graphene/path.c to Pal/lib/path.c. This is to get rid of the graphene/ subdir. All Graphene-specific utility files are stored under Pal/lib/ by convention.

  • [Pal/lib] Remove redundant functionality from crypto. This commit simplifies the crypto code:

    • rng-arch.h header and its get_rand64() are replaced with _DkRandomBitsRead() and subsequently removed.
    • lib_Base64Encode() and lib_Base64Decode() are replaced with their mbedTLS counterparts and subsequently removed.
    • All mbedTLS wrappers are amalgamated in a single mbedtls_adapter.c.
    • pal_crypto.h header is renamed to crypto.h (it has nothing to do with PAL code).

How to test this PR?

All tests must pass.


This change is Reviewable

@dimakuv dimakuv changed the title Dimakuv/misc pal lib fixes [Pal/lib] Remove list-test, move path.c, refactor crypto code May 11, 2021
Copy link
Contributor Author

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 19 files reviewed, all discussions resolved, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: Intel)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 20 at r1 (raw file):

#include "mbedtls/rsa.h"
#include "mbedtls/sha256.h"
#include "pal_error.h"

FYI: I plan to move and rename this pal_error.h header in a next PR. It doesn't make sense for the "common library" to include PAL-specific headers.


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 423 at r1 (raw file):

    return 0;
}

FYI: Below code is copied from mbedtls_dh.c

@dimakuv
Copy link
Contributor Author

dimakuv commented May 11, 2021

Jenkins, retest Jenkins-Debug-20.04 please (kill11 LTP test failed)

Copy link
Contributor

@boryspoplawski boryspoplawski left a comment

Choose a reason for hiding this comment

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

Reviewed 16 of 19 files at r1, 3 of 3 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @dimakuv)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 23 at r2 (raw file):

/* This is declared in pal_internal.h, but that can't be included here. */
int _DkRandomBitsRead(void* buffer, size_t size);

How does this work? Isn't the resulting object file .o included in LibOS binary? Wouldn't that cause LibOS to use PAL internal interface?
(seems to be an existing issue)

Copy link
Contributor Author

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @boryspoplawski)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 23 at r2 (raw file):

Previously, boryspoplawski (Borys Popławski) wrote…

How does this work? Isn't the resulting object file .o included in LibOS binary? Wouldn't that cause LibOS to use PAL internal interface?
(seems to be an existing issue)

Good question. But not, LibOS will not use _DkRandomBitsRead() because this function is only called from mbedTLS wrappers. LibOS doesn't use these wrappers (in particular, the file mbedtls_adapter.o). So during the final linking step, when LibOS sources are linked against static graphene-lib.a, the linker notices that mbedTLS wrappers are not used and removes all that stuff. So in the end, the libsysdb.so library doesn't have any references to _DkRandomBitsRead().

I verified it by manually inspecting the symbols and relocations in the resulting libraries.

This is a pretty bad design decision though... I don't know yet how to fix it. I could keep it as-is for now. (Maybe it's not too bad, after all it's just a callback that the actual user of this helper lib needs to provide.)

yamahata
yamahata previously approved these changes May 12, 2021
Copy link
Contributor

@yamahata yamahata left a comment

Choose a reason for hiding this comment

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

Reviewed 16 of 19 files at r1, 3 of 3 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion, not enough approvals from maintainers (1 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @boryspoplawski)

Copy link
Contributor

@boryspoplawski boryspoplawski left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion, not enough approvals from maintainers (1 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @dimakuv)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 23 at r2 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

Good question. But not, LibOS will not use _DkRandomBitsRead() because this function is only called from mbedTLS wrappers. LibOS doesn't use these wrappers (in particular, the file mbedtls_adapter.o). So during the final linking step, when LibOS sources are linked against static graphene-lib.a, the linker notices that mbedTLS wrappers are not used and removes all that stuff. So in the end, the libsysdb.so library doesn't have any references to _DkRandomBitsRead().

I verified it by manually inspecting the symbols and relocations in the resulting libraries.

This is a pretty bad design decision though... I don't know yet how to fix it. I could keep it as-is for now. (Maybe it's not too bad, after all it's just a callback that the actual user of this helper lib needs to provide.)

Please add a comment about it then (GH issue, in code, wherever).

Copy link
Contributor Author

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion, not enough approvals from maintainers (1 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @boryspoplawski)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 23 at r2 (raw file):

Previously, boryspoplawski (Borys Popławski) wrote…

Please add a comment about it then (GH issue, in code, wherever).

Done. #2371

boryspoplawski
boryspoplawski previously approved these changes May 12, 2021
Copy link
Contributor

@boryspoplawski boryspoplawski left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, all discussions resolved, "fixup! " found in commit messages' one-liners

Copy link
Member

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewed 16 of 19 files at r1, 3 of 3 files at r2.
Reviewable status: all files reviewed, 6 unresolved discussions, "fixup! " found in commit messages' one-liners (waiting on @dimakuv)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 3 at r2 (raw file):

/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2017 Fortanix, Inc. */
/* Copyright (C) 2019, Texas A&M University. */

Could you drop the comma and the period to unify with other places?


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 439 at r2 (raw file):

     * haven't implemented that yet. */
    ret = mbedtls_mpi_read_string(&context->P, 16 /* radix */, MBEDTLS_DHM_RFC3526_MODP_2048_P);
    if (ret < 0)

You changed all != to <, but I think mbedtls returns positive error codes?


Pal/src/host/Linux-SGX/tools/common/attestation.c, line 175 at r2 (raw file):

    ret = mbedtls_base64_decode(ias_sig, ias_sig_size, &ias_sig_size, ias_sig_b64,
                                ias_sig_b64_size);
    if (ret < 0) {

not !=?


Pal/src/host/Linux-SGX/tools/common/attestation.c, line 289 at r2 (raw file):

    ret = mbedtls_base64_decode(report_quote, quote_size, &quote_size, (uint8_t*)node->valuestring,
                                strlen(node->valuestring));
    if (ret < 0) {

ditto


Pal/src/host/Linux-SGX/tools/common/ias.c, line 353 at r2 (raw file):

        ret = mbedtls_base64_decode(*sigrl, *sigrl_size, sigrl_size, (uint8_t*)ias_resp->data,
                                    strlen(ias_resp->data));
        if (ret < 0 || !*sigrl_size) {

ditto


Pal/src/host/Linux-SGX/tools/common/ias.c, line 397 at r2 (raw file):

    ret = mbedtls_base64_encode((uint8_t*)quote_b64, quote_b64_size, &quote_b64_size, quote,
                                quote_size);
    if (ret < 0) {

not !=?

@dimakuv dimakuv dismissed stale reviews from boryspoplawski and yamahata via a21d22b May 14, 2021 08:51
@dimakuv dimakuv force-pushed the dimakuv/misc-pal-lib-fixes branch from 48fbae1 to a21d22b Compare May 14, 2021 08:51
Copy link
Contributor Author

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: 18 of 19 files reviewed, 6 unresolved discussions, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: Intel), "fixup! " found in commit messages' one-liners (waiting on @boryspoplawski, @mkow, and @yamahata)


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 3 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

Could you drop the comma and the period to unify with other places?

Done.


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 439 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

You changed all != to <, but I think mbedtls returns positive error codes?

Why do you think so? MbedTLS returns negative error codes on failures. See e.g. https://tls.mbed.org/api/error_8h.html.

(There are a couple legitimate cases where we check mbedTLS functions via ret != 0 because we don't want positive values sometimes, but these do not denote errors)

Btw, I don't know why the old code contained this != 0. I think this was just a very old convention in the Graphene code.


Pal/src/host/Linux-SGX/tools/common/attestation.c, line 175 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

not !=?

ditto. See e.g. https://tls.mbed.org/api/base64_8h.html#a1e8abeb72a244acb346fc4ac3083886c


Pal/src/host/Linux-SGX/tools/common/attestation.c, line 289 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

ditto

ditto


Pal/src/host/Linux-SGX/tools/common/ias.c, line 353 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

ditto

ditto


Pal/src/host/Linux-SGX/tools/common/ias.c, line 397 at r2 (raw file):

Previously, mkow (Michał Kowalczyk) wrote…

not !=?

ditto

mkow
mkow previously approved these changes May 14, 2021
Copy link
Member

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed, all discussions resolved, not enough approvals from maintainers (1 more required), "fixup! " found in commit messages' one-liners


Pal/lib/crypto/adapters/mbedtls_adapter.c, line 439 at r2 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

Why do you think so? MbedTLS returns negative error codes on failures. See e.g. https://tls.mbed.org/api/error_8h.html.

(There are a couple legitimate cases where we check mbedTLS functions via ret != 0 because we don't want positive values sometimes, but these do not denote errors)

Btw, I don't know why the old code contained this != 0. I think this was just a very old convention in the Graphene code.

Oh, my bad. I saw that all the old code uses != 0 specifically for mbed TLS, then I checked their mbedtls__ecode.h and saw things like #define MBEDTLS_ERR_CMAC_BASE ( MBEDTLS_ECODE_AESDRV_BASE | 0xf00) so I concluded that it's indeed positive :)

@dimakuv
Copy link
Contributor Author

dimakuv commented May 14, 2021

Jenkins, retest Jenkins-Debug-20.04 please (kill11 LTP test failed)

boryspoplawski
boryspoplawski previously approved these changes May 14, 2021
Copy link
Contributor

@boryspoplawski boryspoplawski left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed, all discussions resolved, "fixup! " found in commit messages' one-liners

Signed-off-by: Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
This is to get rid of the `graphene/` subdir. All Graphene-specific
utility files are stored under `Pal/lib/` by convention.

Signed-off-by: Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
This commit simplifies the crypto code:
- `rng-arch.h` header and its `get_rand64()` are replaced with
  `_DkRandomBitsRead()` and subsequently removed.
- `lib_Base64Encode()` and `lib_Base64Decode()` are replaced with their
  mbedTLS counterparts and subsequently removed.
- All mbedTLS wrappers are amalgamated in a single `mbedtls_adapter.c`.
- `pal_crypto.h` header is renamed to `crypto.h` (it has nothing to do
  with PAL code).

Signed-off-by: Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
@dimakuv dimakuv dismissed stale reviews from boryspoplawski and mkow via a70235d May 14, 2021 11:24
@dimakuv dimakuv force-pushed the dimakuv/misc-pal-lib-fixes branch 2 times, most recently from a70235d to adf6269 Compare May 14, 2021 11:25
Copy link
Member

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r4.
Reviewable status: all files reviewed, all discussions resolved, not enough approvals from maintainers (1 more required)

Copy link
Contributor

@boryspoplawski boryspoplawski left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r4.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@boryspoplawski boryspoplawski merged commit adf6269 into master May 14, 2021
@boryspoplawski boryspoplawski deleted the dimakuv/misc-pal-lib-fixes branch May 14, 2021 14:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants