greenbone / gvmd

Greenbone Vulnerability Manager - The database backend for the Greenbone Community Edition
GNU Affero General Public License v3.0
290 stars 157 forks source link

gvmd 11.0.0 sql: (null) when started #805

Closed fcolista closed 4 years ago

fcolista commented 5 years ago

Expected behavior

gvmd postgresql database should be initialized

Current behavior

Database is not initalized, gvmd crashes See the logfile section for further info.

Steps to reproduce

  1. Start postgresql
  2. run:
    su - postgres
    createuser -DRS ovas
    createdb -O ovas gvmd
    psql gvmd
    create role dba with superuser noinherit;
    grant dba to ovas;
    create extension "uuid-ossp";
    exit
  3. as "ovas" user, run gvmd

GVM versions

gsa: (gsad --version) Greenbone Security Assistant 9.0

gvm: (gvmd --version) Greenbone Vulnerability Manager 9.0.0 Manager DB revision 221 Copyright (C) 2010-2017 Greenbone Networks GmbH License GPLv2+: GNU GPL version 2 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

openvas-scanner: (openvassd --version) OpenVAS 7.0.0 Most new code since 2005: (C) 2019 Greenbone Networks GmbH Nessus origin: (C) 2004 Renaud Deraison deraison@nessus.org License GPLv2: GNU GPL version 2 This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

gvm-libs: gvm-libs-11.0.0 openvas-smb:

Environment

Alpine Linux edge (with musl libc)

Installation method / source: (packages, source installation) Source

Logfiles

md   main:MESSAGE:2019-10-16 10h55.24 utc:5356:    Greenbone Vulnerability Manager version 9.0.0 (DB revision 221)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_lock_nb: lock 'gvm-checking'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_locked: check 'gvm-migrating'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_lock_nb: lock 'gvm-migrating'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_unlock: unlock '/var/run/gvm-migrating'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_locked: check 'gvm-helping'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_lock_nb: lock 'gvm-helping'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_unlock: unlock '/var/run/gvm-helping'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5356: lockfile_lock_nb: lock 'gvm-serving'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: polling
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open:   db: gvmd
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: user: ovas
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: host: /tmp
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: port: 5432
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: socket: 7
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_open: postgres version: 120000
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = 'gvmd'               AND table_schema = 'scap'               AND table_name = 'cves') ::integer;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = '%s'               AND table_schema = 'scap'               AND table_name = 'cves') ::integer;)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = 'gvmd'               AND table_schema = 'cert'               AND table_name = 'dfn_cert_advs') ::integer;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = '%s'               AND table_schema = 'cert'               AND table_name = 'dfn_cert_advs') ::integer;)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: lockfile_lock: lock 'gvm-create-functions'
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = 'gvmd'               AND table_schema = 'public'               AND table_name = 'meta')        ::integer;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = '%s'               AND table_schema = 'public'               AND table_name = 'meta')        ::integer;)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT count (*) FROM pg_available_extensions WHERE name = 'uuid-ossp' AND installed_version IS NOT NULL;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT count (*) FROM pg_available_extensions WHERE name = 'uuid-ossp' AND installed_version IS NOT NULL;)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SET role dba;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION hosts_contains (text, text) RETURNS boolean AS '/usr/lib/libgvm-pg-server', 'sql_hosts_contains' LANGUAGE C IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION max_hosts (text, text) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_max_hosts' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION level_max_severity (text, text) RETURNS double precision AS '/usr/lib/libgvm-pg-server', 'sql_level_max_severity' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION level_min_severity (text, text) RETURNS double precision AS '/usr/lib/libgvm-pg-server', 'sql_level_min_severity' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION next_time (integer, integer, integer, integer) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_next_time' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION next_time (integer, integer, integer, integer, text) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_next_time' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION next_time (integer, integer, integer, integer, text, integer) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_next_time' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION next_time_ical (text, text) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_next_time_ical' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION next_time_ical (text, text, integer) RETURNS integer AS '/usr/lib/libgvm-pg-server', 'sql_next_time_ical' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION severity_matches_ov (double precision,                                                double precision) RETURNS boolean AS '/usr/lib/libgvm-pg-server', 'sql_severity_matches_ov' LANGUAGE C IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION valid_db_resource_type (text) RETURNS boolean AS '/usr/lib/libgvm-pg-server', 'sql_valid_db_resource_type' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION regexp (text, text) RETURNS boolean AS '/usr/lib/libgvm-pg-server', 'sql_regexp' LANGUAGE C;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT count(*) FROM pg_operator WHERE oprname = '?~#';
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT count(*) FROM pg_operator WHERE oprname = '?~#';)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OPERATOR ?~# (PROCEDURE = regexp, LEFTARG = text, RIGHTARG = text);
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: RESET role;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION try_exclusive_lock (regclass) RETURNS integer AS $$ BEGIN   EXECUTE 'LOCK TABLE "'           || $1           || '" IN ACCESS EXCLUSIVE MODE NOWAIT;';   RETURN 1; EXCEPTION WHEN lock_not_available THEN   RETURN 0; END;$$ language 'plpgsql';
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = 'gvmd'               AND table_schema = 'public'               AND table_name = 'meta') ::integer;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql_x end (SELECT EXISTS (SELECT * FROM information_schema.tables               WHERE table_catalog = '%s'               AND table_schema = 'public'               AND table_name = 'meta') ::integer;)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION report_progress_active (integer) RETURNS integer AS $$ DECLARE   report_task integer;   task_target integer;   target_hosts text;   target_exclude_hosts text;   progress integer;   total integer;   maximum_hosts integer;   total_progress integer;   report_host record;   dead_hosts integer; BEGIN   total := 0;   dead_hosts := 0;   report_task := (SELECT task FROM reports WHERE id = $1);   task_target := (SELECT target FROM tasks WHERE id = report_task);   IF task_target IS NULL THEN     target_hosts := NULL;     target_exclude_hosts := NULL;   ELSIF (SELECT target_location = 1          FROM tasks WHERE id = report_task)   THEN     target_hosts := (SELECT hosts FROM targets_trash                      WHERE id = task_target);     target_exclude_hosts := (SELECT exclude_hosts FROM targets_trash                              WHERE id = task_target);   ELSE     target_hosts := (SELECT hosts FROM targets                      WHERE id = task_target);     target_exclude_hosts := (SELECT exclude_hosts FROM targets                              WHERE id = task_target);   END IF;   IF target_hosts IS NULL THEN     RETURN 0;   END IF;   maximum_hosts := max_hosts (target_hosts, target_exclude_hosts);   IF maximum_hosts = 0 THEN     RETURN 0;   END IF;   FOR report_host IN SELECT current_port, max_port                      FROM report_hosts WHERE report = $1   LOOP     IF report_host.max_port = -1 THEN       progress := 0;       dead_hosts := dead_hosts + 1;     ELSEIF report_host.max_port IS NOT NULL        AND report_host.max_port != 0     THEN       progress := (report_host.current_port * 100)                   / report_host.max_port;     ELSIF report_host.current_port IS NULL           OR report_host.current_port = 0     THEN       progress := 0;     ELSE       progress := 100;     END IF;     total := total + progress;   END LOOP;   IF (maximum_hosts - dead_hosts) > 0 THEN     total_progress := total / (maximum_hosts - dead_hosts);   ELSE     total_progress := 0;   END IF;   IF total_progress = 0 THEN     RETURN 1;   ELSIF total_progress = 100 THEN     RETURN 99;   END IF;   RETURN total_progress; END;$$ LANGUAGE plpgsql;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION order_inet (text) RETURNS text AS $$ BEGIN   IF $1 ~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' THEN     RETURN chr (1)            || to_char (split_part ($1, '.', 1)::integer, 'fm000')            || '.'            || to_char (split_part ($1, '.', 2)::integer, 'fm000')            || '.'            || to_char (split_part ($1, '.', 3)::integer, 'fm000')            || '.'            || to_char (split_part ($1, '.', 4)::integer, 'fm000');   ELSE     RETURN $1;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION order_message_type (text) RETURNS integer AS $$ BEGIN   IF $1 = 'Security Hole' THEN     RETURN 1;   ELSIF $1 = 'Security Warning' THEN     RETURN 2;   ELSIF $1 = 'Security Note' THEN     RETURN 3;   ELSIF $1 = 'Log Message' THEN     RETURN 4;   ELSIF $1 = 'Debug Message' THEN     RETURN 5;   ELSIF $1 = 'Error Message' THEN     RETURN 6;   ELSE     RETURN 7;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION order_port (text) RETURNS integer AS $$ BEGIN   IF $1 ~ '^[0-9]+' THEN     RETURN CAST (substring ($1, '^[0-9]+') as integer);   ELSIF $1 ~ '^[^0-9]* \([0-9]+/' THEN     RETURN CAST (substring ($1, '^[^0-9]* \(([0-9]+)/') as integer);   ELSE     RETURN 0;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION order_role (text) RETURNS text AS $$ BEGIN   IF $1 = 'Admin' THEN     RETURN ' !';   ELSE     RETURN $1;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: CREATE OR REPLACE FUNCTION order_threat (text) RETURNS integer AS $$ BEGIN   IF $1 = 'High' THEN     RETURN 1;   ELSIF $1 = 'Medium' THEN     RETURN 2;   ELSIF $1 = 'Low' THEN     RETURN 3;   ELSIF $1 = 'Log' THEN     RETURN 4;   ELSIF $1 = 'Debug' THEN     RETURN 5;   ELSIF $1 = 'False Positive' THEN     RETURN 6;   ELSIF $1 = 'None' THEN     RETURN 7;   ELSE     RETURN 8;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357:    sql: (null)
md manage:  DEBUG:2019-10-16 10h55.24 utc:5357: sql_exec_internal: sqlstate: (null)
md manage:WARNING:2019-10-16 10h55.24 utc:5357: sql_exec_internal: PQexec failed:  (7)
md manage:WARNING:2019-10-16 10h55.24 utc:5357: sql_exec_internal: SQL: (null)
md manage:WARNING:2019-10-16 10h55.24 utc:5357: sqlv: sql_exec_internal failed

.: Francesco

fcolista commented 5 years ago

I've enabled postgresql log, the last query done is:

2019-10-17 13:32:06.603 UTC [7019] LOG:  execute <unnamed>: CREATE OR REPLACE FUNCTION order_threat (text) RETURNS integer AS $$ BEGIN   IF $1 = 'High' THEN     RETURN 1;   ELSIF $1 = 'Medium' THEN     RETURN 2;   ELSIF $1 = 'Low' THEN     RETURN 3;   ELSIF $1 = 'Log' THEN     RETURN 4;   ELSIF $1 = 'Debug' THEN     RETURN 5;   ELSIF $1 = 'False Positive' THEN     RETURN 6;   ELSIF $1 = 'None' THEN     RETURN 7;   ELSE     RETURN 8;   END IF; END;$$ LANGUAGE plpgsql IMMUTABLE;

Looking at manage_pg.c file, on lines 905-927, this is the last query run:

  sql ("CREATE OR REPLACE FUNCTION order_threat (text)"
       " RETURNS integer AS $$"
       " BEGIN"
       "   IF $1 = 'High' THEN"
       "     RETURN 1;"
       "   ELSIF $1 = 'Medium' THEN"
       "     RETURN 2;"
       "   ELSIF $1 = 'Low' THEN"
       "     RETURN 3;"
       "   ELSIF $1 = 'Log' THEN"
       "     RETURN 4;"
       "   ELSIF $1 = 'Debug' THEN"
       "     RETURN 5;"
       "   ELSIF $1 = 'False Positive' THEN"
       "     RETURN 6;"
       "   ELSIF $1 = 'None' THEN"
       "     RETURN 7;"
       "   ELSE"
       "     RETURN 8;"
       "   END IF;"
       " END;"
       "$$ LANGUAGE plpgsql"
       " IMMUTABLE;");

If I try to ran by hand, no issues are returned. The next one (which appears to be the one giving sql: (null) result, is:

  sql ("CREATE OR REPLACE FUNCTION severity_to_type (double precision)"
       " RETURNS text AS $$"
       " BEGIN"
       "   IF $1 IS NULL THEN"
       "     RETURN NULL;"
       "   ELSIF $1 = " G_STRINGIFY (SEVERITY_LOG) " THEN"
       "     RETURN 'Log Message';"
       "   ELSIF $1 = " G_STRINGIFY (SEVERITY_FP) " THEN"
       "     RETURN 'False Positive';"
       "   ELSIF $1 = " G_STRINGIFY (SEVERITY_DEBUG) " THEN"
       "     RETURN 'Debug Message';"
       "   ELSIF $1 = " G_STRINGIFY (SEVERITY_ERROR) " THEN"
       "     RETURN 'Error Message';"
       "   ELSIF $1 > 0.0 AND $1 <= 10.0 THEN"
       "     RETURN 'Alarm';"
       "   ELSE"
       "     RAISE EXCEPTION 'Invalid severity score given: %', $1;"
       "   END IF;"
       " END;"
       "$$ LANGUAGE plpgsql"
       " IMMUTABLE;");

But also this query, if ran by "hand", it works nicely.

openvas:~$ psql gvmd
psql (12.0)
Type "help" for help.

gvmd=> CREATE OR REPLACE FUNCTION severity_to_type (double precision)
gvmd-> RETURNS text AS $$
gvmd$> BEGIN
gvmd$> IF $1 IS NULL THEN
gvmd$> RETURN NULL;
gvmd$> ELSIF $1 = " G_STRINGIFY (SEVERITY_LOG) " THEN
gvmd$> RETURN 'Log Message';
gvmd$> ELSIF $1 = " G_STRINGIFY (SEVERITY_FP) " THEN
gvmd$> RETURN 'False Positive';
gvmd$> ELSIF $1 = " G_STRINGIFY (SEVERITY_DEBUG) " THEN
gvmd$> RETURN 'Debug Message';
gvmd$> ELSIF $1 = " G_STRINGIFY (SEVERITY_ERROR) " THEN
gvmd$> RETURN 'Error Message';
gvmd$> ELSIF $1 > 0.0 AND $1 <= 10.0 THEN
gvmd$> RETURN 'Alarm';
gvmd$> ELSE
gvmd$> RAISE EXCEPTION 'Invalid severity score given: %', $1;
gvmd$> END IF;
gvmd$> END;
gvmd$> $$ LANGUAGE plpgsql IMMUTABLE;
CREATE FUNCTION

.: Francesco

cfi-gb commented 4 years ago

Fix was merged in #818 , closing.