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

Create persistence.h #60

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions persistence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef PERSISTENCE_H
#define PERSISTENCE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <openssl/rsa.h>
#include "io_socket.h"
#include "pml_logic_loop.h"
#include "memory_silo.h"
#include "knowledge.h"
#include "PMLL_ARLL_EFLL.h"

// Constants
#define MAX_KNOWLEDGE_NODES 4096
#define SESSION_TIMEOUT_MS 5000
#define ENCRYPTION_KEY_SIZE 2048
#define PACKAGE_TYPE_KNOWLEDGE_UPDATE 1

// Session context structure
typedef struct {
io_socket_t io_socket;
PMLL_ARLL_EFLL_State state;
Graph *knowledge_graph;
RSA *encryption_key;
memory_silo_t *persistent_silo;
} SessionContext;
Comment on lines +22 to +29
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add documentation and clarify memory ownership.

The SessionContext structure needs:

  1. Documentation for each field explaining its purpose and lifecycle
  2. Clear ownership semantics for pointer members
  3. Initialization requirements and cleanup responsibilities
 // Session context structure
+/**
+ * @brief Encapsulates the state and resources for a session
+ * 
+ * Memory ownership:
+ * - knowledge_graph: Owned by the session, must be freed in destroy_session
+ * - encryption_key: Owned by the session, must be freed using RSA_free
+ * - persistent_silo: External ownership, must not be freed by the session
+ */
 typedef struct {
     io_socket_t io_socket;
     PMLL_ARLL_EFLL_State state;
     Graph *knowledge_graph;
     RSA *encryption_key;
     memory_silo_t *persistent_silo;
 } SessionContext;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Session context structure
typedef struct {
io_socket_t io_socket;
PMLL_ARLL_EFLL_State state;
Graph *knowledge_graph;
RSA *encryption_key;
memory_silo_t *persistent_silo;
} SessionContext;
/**
* @brief Encapsulates the state and resources for a session
*
* Memory ownership:
* - knowledge_graph: Owned by the session, must be freed in destroy_session
* - encryption_key: Owned by the session, must be freed using RSA_free
* - persistent_silo: External ownership, must not be freed by the session
*/
typedef struct {
io_socket_t io_socket;
PMLL_ARLL_EFLL_State state;
Graph *knowledge_graph;
RSA *encryption_key;
memory_silo_t *persistent_silo;
} SessionContext;


// Function declarations
SessionContext* create_session(const char *ip, int port,
int max_retries, int feedback_threshold,
const char *encryption_key_file);
void process_chatlog(SessionContext *ctx, const char *input);
void send_secure_graph_update(SessionContext *ctx);
void save_session_state(SessionContext *ctx, const char* filename);
void handle_network_error(SessionContext *ctx);
void run_session_loop(SessionContext *ctx);
Comment on lines +31 to +39
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve function signatures for error handling and thread safety.

Core functions need:

  1. Error return values for failure cases
  2. Const correctness for input parameters
  3. Documentation for thread safety guarantees
-SessionContext* create_session(const char *ip, int port, 
-                              int max_retries, int feedback_threshold,
-                              const char *encryption_key_file);
-void process_chatlog(SessionContext *ctx, const char *input);
-void send_secure_graph_update(SessionContext *ctx);
-void save_session_state(SessionContext *ctx, const char* filename);
-void handle_network_error(SessionContext *ctx);
-void run_session_loop(SessionContext *ctx);
+/**
+ * @brief Creates a new session with the specified parameters
+ * @return NULL on failure with errno set
+ * @thread_safety Thread-unsafe
+ */
+SessionContext* create_session(const char *ip, int port,
+                             int max_retries, int feedback_threshold,
+                             const char *encryption_key_file);
+
+/**
+ * @brief Processes chat log input
+ * @return 0 on success, negative error code on failure
+ * @thread_safety Thread-safe
+ */
+int process_chatlog(SessionContext * restrict ctx, const char * restrict input);

Committable suggestion skipped: line range outside the PR's diff.


// Utility function declarations (assumed to be implemented elsewhere)
RSA* load_rsa_key(const char *filename);
RSA* generate_rsa_key(int key_size);
void save_rsa_key(const char *filename, RSA *key);
char* sanitize_input(const char *input);
EncryptedNode* encrypt_node_data(RSA *key, const char *data);
int add_encrypted_node(Graph *graph, EncryptedNode *enode);
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash);
unsigned char* serialize_graph(Graph *graph, size_t *size);
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size,
unsigned char *encrypted, size_t encrypted_size);
unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size,
size_t *encrypted_size);
unsigned int calculate_checksum(const unsigned char *data, size_t size);
Comment on lines +41 to +54
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Address security concerns in encryption functions.

The encryption-related functions need:

  1. Buffer size parameters to prevent overflows
  2. Error handling for encryption failures
  3. Secure memory handling for sensitive data
-unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size, 
-                                  size_t *encrypted_size);
+/**
+ * @brief Encrypts data using RSA with secure memory handling
+ * @param key RSA key for encryption
+ * @param data Input data to encrypt
+ * @param data_size Size of input data
+ * @param max_output_size Maximum size of the output buffer
+ * @param encrypted_size [out] Actual size of encrypted data
+ * @return Encrypted data in secure memory (must be freed with OPENSSL_clear_free)
+ */
+unsigned char* rsa_encrypt_buffer(RSA *key,
+                                const unsigned char *data,
+                                size_t data_size,
+                                size_t max_output_size,
+                                size_t *encrypted_size);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Utility function declarations (assumed to be implemented elsewhere)
RSA* load_rsa_key(const char *filename);
RSA* generate_rsa_key(int key_size);
void save_rsa_key(const char *filename, RSA *key);
char* sanitize_input(const char *input);
EncryptedNode* encrypt_node_data(RSA *key, const char *data);
int add_encrypted_node(Graph *graph, EncryptedNode *enode);
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash);
unsigned char* serialize_graph(Graph *graph, size_t *size);
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size,
unsigned char *encrypted, size_t encrypted_size);
unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size,
size_t *encrypted_size);
unsigned int calculate_checksum(const unsigned char *data, size_t size);
// Utility function declarations (assumed to be implemented elsewhere)
RSA* load_rsa_key(const char *filename);
RSA* generate_rsa_key(int key_size);
void save_rsa_key(const char *filename, RSA *key);
char* sanitize_input(const char *input);
EncryptedNode* encrypt_node_data(RSA *key, const char *data);
int add_encrypted_node(Graph *graph, EncryptedNode *enode);
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash);
unsigned char* serialize_graph(Graph *graph, size_t *size);
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size,
unsigned char *encrypted, size_t encrypted_size);
/**
* @brief Encrypts data using RSA with secure memory handling
* @param key RSA key for encryption
* @param data Input data to encrypt
* @param data_size Size of input data
* @param max_output_size Maximum size of the output buffer
* @param encrypted_size [out] Actual size of encrypted data
* @return Encrypted data in secure memory (must be freed with OPENSSL_clear_free)
*/
unsigned char* rsa_encrypt_buffer(RSA *key,
const unsigned char *data,
size_t data_size,
size_t max_output_size,
size_t *encrypted_size);
unsigned int calculate_checksum(const unsigned char *data, size_t size);

void write_file_atomically(const char *filename, const unsigned char *data, size_t size);
unsigned int calculate_graph_hash(Graph *graph);
memory_silo_t* connect_memory_silo(int silo_type);
void load_initial_knowledge(SessionContext *ctx, const char *filename);
void perform_garbage_collection(Graph *graph);
void optimize_memory_usage(memory_silo_t *silo);
void initiate_graceful_shutdown(SessionContext *ctx);
void validate_session_integrity(SessionContext *ctx);
int initialize_subsystems(SessionContext *ctx);
int process_queued_messages(SessionContext *ctx);
void handle_runtime_error(SessionContext *ctx);
int attempt_state_recovery(SessionContext *ctx);
void destroy_session(SessionContext *ctx);
Comment on lines +55 to +67
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve modularity and error handling in utility functions.

Consider:

  1. Splitting utilities into logical modules (e.g., crypto, persistence, memory)
  2. Adding error handling for all critical operations
  3. Documenting cleanup requirements

These functions should be moved to separate header files based on their functionality:

  • crypto.h: RSA and encryption-related functions
  • persistence.h: File and state management
  • memory.h: Memory silo and garbage collection

This would improve maintainability and make dependencies clearer.


#endif // PERSISTENCE_H
Comment on lines +1 to +69
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Security concerns found in dependent components

The header dependencies are satisfied, but there are security concerns:

  • Multiple instances of unsafe string functions (strcpy, strcat, sprintf, gets, scanf) found in the codebase
  • These functions are vulnerable to buffer overflows and should be replaced with safer alternatives:
    • Use strncpy, strncat, snprintf instead
    • Replace gets with fgets
    • Use scanf with field width limits or safer alternatives
🔗 Analysis chain

Verify header dependencies and security requirements.

Please ensure all required headers are available and security requirements are met.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify header dependencies and OpenSSL version

# Check for required headers
echo "Checking for required headers..."
for header in io_socket.h pml_logic_loop.h memory_silo.h knowledge.h PMLL_ARLL_EFLL.h; do
  if ! fd -t f "$header"; then
    echo "Missing required header: $header"
  fi
done

# Check OpenSSL version and security requirements
echo "Checking OpenSSL version..."
if command -v pkg-config &> /dev/null; then
  pkg-config --print-requires openssl
  pkg-config --modversion openssl
fi

# Check for potential security issues in the codebase
echo "Checking for potential security issues..."
rg -i "strcpy|strcat|sprintf|gets|scanf" || echo "No unsafe string functions found"

Length of output: 3706

Loading