Skip to content

Commit a13fcec

Browse files
committed
libbpf: Implement SHA256 internal helper
Use AF_ALG sockets to not have libbpf depend on OpenSSL. The helper is used for the loader generation code to embed the metadata hash in the loader program and also by the bpf_map__make_exclusive API to calculate the hash of the program the map is exclusive to. Signed-off-by: KP Singh <[email protected]>
1 parent a961194 commit a13fcec

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#include <sys/vfs.h>
4444
#include <sys/utsname.h>
4545
#include <sys/resource.h>
46+
#include <sys/socket.h>
47+
#include <linux/if_alg.h>
48+
#include <linux/socket.h>
4649
#include <libelf.h>
4750
#include <gelf.h>
4851
#include <zlib.h>
@@ -14207,3 +14210,59 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
1420714210
free(s->progs);
1420814211
free(s);
1420914212
}
14213+
14214+
int libbpf_sha256(const void *data, size_t data_sz, void *sha_out, size_t sha_out_sz)
14215+
{
14216+
struct sockaddr_alg sa = {
14217+
.salg_family = AF_ALG,
14218+
.salg_type = "hash",
14219+
.salg_name = "sha256"
14220+
};
14221+
int sock_fd = -1;
14222+
int op_fd = -1;
14223+
int err = 0;
14224+
14225+
if (sha_out_sz != SHA256_DIGEST_LENGTH) {
14226+
pr_warn("sha_out_sz should be exactly 32 bytes for a SHA256 digest");
14227+
return libbpf_err(-EINVAL);
14228+
}
14229+
14230+
sock_fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
14231+
if (sock_fd < 0) {
14232+
err = -errno;
14233+
pr_warn("failed to create AF_ALG socket for SHA256: %s\n", errstr(err));
14234+
return libbpf_err(err);
14235+
}
14236+
14237+
if (bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
14238+
err = -errno;
14239+
pr_warn("failed to bind to AF_ALG socket for SHA256: %s\n", errstr(err));
14240+
goto out;
14241+
}
14242+
14243+
op_fd = accept(sock_fd, NULL, 0);
14244+
if (op_fd < 0) {
14245+
err = -errno;
14246+
pr_warn("failed to accept from AF_ALG socket for SHA256: %s\n", errstr(err));
14247+
goto out;
14248+
}
14249+
14250+
if (write(op_fd, data, data_sz) != data_sz) {
14251+
err = -errno;
14252+
pr_warn("failed to write data to AF_ALG socket for SHA256: %s\n", errstr(err));
14253+
goto out;
14254+
}
14255+
14256+
if (read(op_fd, sha_out, SHA256_DIGEST_LENGTH) != SHA256_DIGEST_LENGTH) {
14257+
err = -errno;
14258+
pr_warn("failed to read SHA256 from AF_ALG socket: %s\n", errstr(err));
14259+
goto out;
14260+
}
14261+
14262+
out:
14263+
if (op_fd >= 0)
14264+
close(op_fd);
14265+
if (sock_fd >= 0)
14266+
close(sock_fd);
14267+
return libbpf_err(err);
14268+
}

tools/lib/bpf/libbpf_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,4 +736,8 @@ int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
736736

737737
int probe_fd(int fd);
738738

739+
#define SHA256_DIGEST_LENGTH 32
740+
#define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64)
741+
742+
int libbpf_sha256(const void *data, size_t data_sz, void *sha_out, size_t sha_out_sz);
739743
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */

0 commit comments

Comments
 (0)